我使用以下示例代码,并希望以某种方式获取sqlite3数据库的列名:
我只得到了以下输出结果: "cid name type notnull dflt_value pk" 但是我没有得到真正的列名。 我需要列名,因为我根据另一个服务器API的结果向数据库写入新的列。在打印数据时,我还需要确保正确的列名被显示。
using System;
using System.Data.SQLite;
namespace Program
{
class Program
{
static void Main(string[] args)
{
Program stuff = new Program();
stuff.DoStuff();
Console.Read();
}
private void DoStuff()
{
SQLiteConnection.CreateFile("Database.sqlite");
SQLiteConnection con = new SQLiteConnection("Data Source=Database.sqlite;Version=3;");
con.Open();
string sql = "create table 'member' ('account_id' text not null unique, 'account_name' text not null);";
SQLiteCommand command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();
sql = "insert into member ('account_id', 'account_name') values ('0', '1');";
command = new SQLiteCommand(sql, con);
sql = "PRAGMA table_info('member');";
command = new SQLiteCommand(sql, con);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetName(0));
}
con.Close();
}
}
}
我也尝试过
for(int i=0;i<reader.FieldCount;i++)
{
Console.WriteLine(reader.GetName(i));
}
var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();
我只得到了以下输出结果: "cid name type notnull dflt_value pk" 但是我没有得到真正的列名。 我需要列名,因为我根据另一个服务器API的结果向数据库写入新的列。在打印数据时,我还需要确保正确的列名被显示。