用NuGet包管理器,可以很方便安装sqlite到项目中,然后引用
using System.Data.SQLite;
第一步用SQLiteConnection建立连接。
第二步用SQLiteCommand操作数据库,增删改等。
第三步用SQLiteDataReader,查询数据库。
完整代码如下
using (SQLiteConnection conn = new SQLiteConnection())
{
string dbPath = "Data Source=" + Environment.CurrentDirectory + @"\Actress.db";
conn.ConnectionString = dbPath;
conn.Open();
using (SQLiteCommand cmd = new SQLiteCommand(conn))
{
cmd.CommandText = @"create table if not exists Actress
(ID integer primary key autoincrement,
Name text not null,
Age integer not null)";
cmd.ExecuteNonQuery();
cmd.CommandText = @"insert into Actress values
(null,'王菲',47),
(null,'范冰冰',37),
(null,'柳岩',36)";
cmd.ExecuteNonQuery();
cmd.CommandText = @"select * from Actress";
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while(reader.Read())
{
Console.WriteLine("ID: " + reader.GetInt64(0));
Console.WriteLine("Name " + reader.GetString(1));
Console.WriteLine("Age " + reader.GetInt32(2));
}
}
}
}
}
Console.ReadKey();
操作上似乎比python