是否可以使用 C# 在 SQLite 中获取列名(标题)? [英] Is it possible to get column name (header) in SQLite using C#?

查看:91
本文介绍了是否可以使用 C# 在 SQLite 中获取列名(标题)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果表和列是在sqlite中生成的代码,是否可以获取列名(标题)?

Is it possible to get column names(Header) if table and columns are generated code'behind in sqlite?

试过了,但失败了:

SQLiteCommand cmd = new SQLiteCommand();

string sSQL = "Select * from tblUser Where username = '" + txtUsername.Text + "'";
cmd.CommandText = sSQL;
cmd.Connection = clsCon.con;
SQLiteDataReader dr2;
dr2 = cmd.ExecuteReader();
string columnName = dr2.GetName(1);
dr2.Read();

if (dr2.HasRows)
{
    MessageBox.Show("Username Already Exist!", "SQLite Test Application", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    txtUsername.Focus();
}

推荐答案

1) 确保数据库是打开的

1) make sure the db is open

2) 确保命令与连接挂钩

2) make sure the command is hooked up with the connection

3) 确保您没有收到任何错误

3) make sure you are not getting any errors

4) 遍历列名

var cmd = new SQLiteCommand("select * from t1", db);
var dr = cmd.ExecuteReader();
for (var i = 0; i < dr.FieldCount; i++)
{
    Console.WriteLine(dr.GetName(i));
}

这篇关于是否可以使用 C# 在 SQLite 中获取列名(标题)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆