HA

在C#项目中使用sqlite

在C#项目中使用sqlite

引入文件

从官网下载了几个dll文件都有问题之后,最终放弃,使用VS自带的NuGet包管理工具,搜索安装即可,没有杂七杂八的问题

列一下关键步骤

String sql = @"create table if not exists user(
username varchar(40),
passward varchar(40)
);
insert into user(username, passward) values({0}, {1});
select * from user;";
string datasource = "test.db";
SQLiteConnection connection = null;
//#1
Connection = new SQLiteConnection("data source = " + this.Datasource);
if (System.IO.File.Exists(Datasource)) {
  SQLiteConnection.CreateFile(datasource);
}
Connection.Open();
//#2
var tb = new System.Data.DataTable();
SQLiteCommand command = new SQLiteCommand(s.Connection);
SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
command.CommandText = String.Format(sql, username.Quote(), password.Quote());
adapter.Fill(tb);
SharpLearn.Program.ShowTable(tb);
//...
//utils
public static void ShowTable(DataTable tb) {
    System.IO.StreamWriter sw = new System.IO.StreamWriter("data.txt");
    foreach (DataColumn col in tb.Columns) {
        sw.Write("{0,-20}", col.ColumnName);
    }
    sw.WriteLine();
    foreach (DataRow dr in tb.Rows) {
        foreach (object o in dr.ItemArray) {
            sw.Write("{0,-20}", o.ToString());
        }
        sw.WriteLine();
    }
    sw.Close();
}

reference__