death114 发表于 2016-11-29 08:24:04

[转]C#与SQLite的操作介绍

  1、通过Add References引用SQLite ADO .NET安装目录的bin目录下的System.Data.SQLite.DLL。
  2、创建数据库文件:因为始终是个0字节文件,应该利用IO也可以(?!)。
  System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
3、连接数据库
  System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString);
connectionString中包含了数据库的一些配置信息,比如数据库文件,数据库打开的密码等,可以利用System.Data.SQLite.SQLiteConnectionStringBuilder来辅助创建connectionString
  4、创建表、读取数据等和Access或MS SQL没多大区别了。


http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gif//创建一个数据库文件
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifstring datasource="h:/test.db";
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifSystem.Data.SQLite.SQLiteConnection.CreateFile(datasource);
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gif//连接数据库
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifSystem.Data.SQLite.SQLiteConnection conn =
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifnew System.Data.SQLite.SQLiteConnection();
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifSystem.Data.SQLite.SQLiteConnectionStringBuilder connstr =
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifnew System.Data.SQLite.SQLiteConnectionStringBuilder();
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifconnstr.DataSource = datasource;
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifconnstr.Password = "admin";//设置密码,SQLite ADO.NET实现了数据库密码保护
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifconn.ConnectionString = connstr.ToString();            
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifconn.Open();
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gif//创建表
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifSystem.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifstring sql = "CREATE TABLE test(username varchar(20),password varchar(20))";
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifcmd.CommandText=sql;
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifcmd.Connection=conn;
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifcmd.ExecuteNonQuery();
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gif//插入数据
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifsql = "INSERT INTO test VALUES(’dotnetthink’,'mypassword’)";
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifcmd.CommandText = sql;
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifcmd.ExecuteNonQuery();
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gif//取出数据
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifsql = "SELECT * FROM test";
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifcmd.CommandText = sql;
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifSystem.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifStringBuilder sb = new StringBuilder();
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifwhile (reader.Read())
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/ExpandedBlockStart.gifhttp://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/ContractedBlock.gif...{
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/InBlock.gif    sb.Append("username:").Append(reader.GetString(0)).Append("\n")
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/InBlock.gif    .Append("password:").Append(reader.GetString(1));
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/ExpandedBlockEnd.gif}
http://www.csharpwin.com/Admin/fckeditor/editor/plugins/insertcode/images/None.gifMessageBox.Show(sb.ToString());
页: [1]
查看完整版本: [转]C#与SQLite的操作介绍