时间:2021-07-01 10:21:17 帮助过:5人阅读
3、连接数据库
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString);connectionString中包含了数据库的一些配置信息,比如数据库文件,数据库打开的密码等,可以利用System.Data.SQLite.SQLiteConnectionStringBuilder来辅助创建connectionString
4、创建表、读取数据等和Access或MS SQL没多大区别了
//创建一个数据库文件 string datasource="h:/test.db"; System.Data.SQLite.SQLiteConnection.CreateFile(datasource); //连接数据库 System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(); System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder(); connstr.DataSource = datasource; connstr.Password = "admin";//设置密码,SQLite ADO.NET实现了数据库密码保护 conn.ConnectionString = connstr.ToString(); conn.Open(); //创建表 System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(); string sql = "CREATE TABLE test(username varchar(20),password varchar(20))"; cmd.CommandText=sql; cmd.Connection=conn; cmd.ExecuteNonQuery(); //插入数据 sql = "INSERT INTO test VALUES(‘ekinglong‘,‘mypassword‘)"; cmd.CommandText = sql; cmd.ExecuteNonQuery(); //取出数据 sql = "SELECT * FROM test"; cmd.CommandText = sql; System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader(); StringBuilder sb = new StringBuilder(); while (reader.Read())
【转载在 .NET 里面使用 SQLite】
标签: