Sqlite在C#中的應(yīng)用
在 .NET 里面使用 SQLite, 我這里使用的wrapper是 System.Data.SQLite,它只需要一個dll,接口符合ADO.Net 2.0的定義,性能也不錯,NHibernate用的也是它,目前支持ADO.NET 3.5了,支持集成在 VS2005 和 VS2008里面,而且支持wince,是個亮點(diǎn)
因?yàn)榉螦DO.NET的規(guī)范,所以使用方式,基本和 SqlClient, OleDb等原生的一致
using System.Data;
using System.Data.SQLite;
//...
using (SQLiteConnection cn = new SQLiteConnection(
"Data Source=Test.db3;Pooling=true;FailIfMissing=false")
)
//Pooling設(shè)置為true時(shí),SQL連接將從連接池獲得,如果沒有則新建并添加到連接池中,默認(rèn)是true。
//FailIfMissing默認(rèn)為false,如果數(shù)據(jù)庫文件不存在,會自動創(chuàng)建一個新的,若設(shè)置為true,將不會創(chuàng)建,而是拋出異常信息。
{
//在打開數(shù)據(jù)庫時(shí),會判斷數(shù)據(jù)庫是否存在,如果不存在,則在當(dāng)前目錄下創(chuàng)建一個
cn.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = cn;
//建立表,如果表已經(jīng)存在,則報(bào)錯
cmd.CommandText = "CREATE TABLE [test] (id int, name nvarchar(20))";
cmd.ExecuteNonQuery();
//插入測試數(shù)據(jù)
for (int i = 2; i < 5; i++)
{
cmd.CommandText = string.Format("INSERT INTO [test] VALUES ({0}, '杜思波技術(shù)討論區(qū)域')", i);
cmd.ExecuteNonQuery();
}
for (int i = 5; i < 10; i++)
{
cmd.CommandText = string.Format("INSERT INTO [test] VALUES ({0}, 'English Test')", i);
cmd.ExecuteNonQuery();
}
//讀取數(shù)據(jù)
cmd.CommandText = "SELECT * FROM [test]";
using (SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (dr.Read())
{
Console.WriteLine("第{0} 條:{1}", dr.GetValue(0), dr.GetString(1));
}
}
}
}
在C#中使用SQLite
1、通過Add References引用SQLite ADO .NET安裝目錄的bin目錄下的System.Data.SQLite.DLL。
2、創(chuàng)建數(shù)據(jù)庫文件:因?yàn)槭冀K是個0字節(jié)文件,應(yīng)該利用IO也可以(??。?。
System.Data.SQLite.SQLiteConnection.CreateFile(datasource);3、連接數(shù)據(jù)庫
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString);connectionString中包含了數(shù)據(jù)庫的一些配置信息,比如數(shù)據(jù)庫文件,數(shù)據(jù)庫打開的密碼等,可以利用System.Data.SQLite.SQLiteConnectionStringBuilder來輔助創(chuàng)建connectionString
4、創(chuàng)建表、讀取數(shù)據(jù)等和Access或MS SQL沒多大區(qū)別了
//創(chuàng)建一個數(shù)據(jù)庫文件
string datasource="h:/test.db";
System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
//連接數(shù)據(jù)庫
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";//設(shè)置密碼,SQLite ADO.NET實(shí)現(xiàn)了數(shù)據(jù)庫密碼保護(hù)
conn.ConnectionString = connstr.ToString();
conn.Open();
//創(chuàng)建表
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();
//插入數(shù)據(jù)
sql = "INSERT INTO test VALUES('ekinglong','mypassword')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
//取出數(shù)據(jù)
sql = "SELECT * FROM test";
cmd.CommandText = sql;
System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (reader.Read())
{
sb.Append("username:").Append(reader.GetString(0)).Append("\n")
.Append("password:").Append(reader.GetString(1));
}
MessageBox.Show(sb.ToString());