SQLite屬于一個輕量級的數據庫,但是功能絕不遜于SQl Server 、Access等
1.去網上下載一個System.Data.SQLite.DLL在要用項目中添加引用,再打開命名空間using System.Data.SQLite;
下載地址:http://files.cnblogs.com/sjrhero/System.Data.SQLite.rar
2.下面就是下載一個SQLite工具我使用的SQLite工具是SQLiteManager,創(chuàng)建一個數據庫擴展名為*.sqlite,再創(chuàng)建表跟SQL Server的Sql語句基本上是一樣的(目前沒有發(fā)現在不一樣,還是第一次用)
3.使用創(chuàng)建的SQLite數據庫(其實跟使用SQL Server數據庫沒有什么區(qū)別,第一次使用)以下是第一次使用的代碼:
protected void Button1_Click(object sender, EventArgs e) { SQLiteConnection sqlcon = new SQLiteConnection("Data Source=|DataDirectory|mydatabase.sqlite;Pooling=true;FailIfMissing=false"); sqlcon.Open(); SQLiteCommand cmd = new SQLiteCommand("select * from testTB where userName = '用戶名'", sqlcon); SQLiteDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { Response.Write(sdr[0].ToString()+"<br />"); } sdr.Close(); //以下是使用SQLiteHelper DataTable dt = SQLiteHelper.ExecuteDataset(sqlcon, "select * from testTB where userName = '用戶名'").Tables[0]; if (dt.Rows.Count>0) { foreach (DataRow item in dt.Rows) { Response.Write(item["userName"].ToString()); } } dt.Dispose(); sqlcon.Close(); }
上面的Data Source=|DataDirectory|mydatabase.sqlite這是使用的相對路徑;將它放在Web應用項目的App_Data目錄,|DataDirectory| 就代表這個目錄的位置,后面的就是文件名(兩堅也是必須帶上)。目前只在B/S模式下測試路徑問題,絕對路徑就不用帶|DataDirectory|了。C/S模式下還沒有驗證 示例如下:
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|
AttachDbFilename=|DataDirectory| 這個直接定位App_Data文件夾。
下面是在web.config中配置的使用
<configuration> <configSections> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null /> </configSections> <dataConfiguration defaultDatabase=" "> <providerMappings> <add databaseType="EntLibContrib.Data.SQLite.SQLiteDatabase, EntLibContrib.Data.SqLite, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null" name="System.Data.SQLite" /> </providerMappings> </dataConfiguration> <connectionStrings> <add name="sqlite" connectionString="Data Source=|DataDirectory|\db;Pooling=true;FailIfMissing=false" providerName="System.Data.SQLite" /> </connectionStrings> </configuration>
以下是別人初學的時候的示例:
private void button1_Click(object sender, EventArgs e) { //數據庫路徑 string datasource = "data.db"; //創(chuàng)建數據庫 SQLiteConnection.CreateFile(datasource); //創(chuàng)建連接,默認密碼為admin SQLiteConnection conn = new SQLiteConnection("Data Source=data.db;password=admin"); //建立連接 conn.Open(); //創(chuàng)建命令 SQLiteCommand cmd = new SQLiteCommand(); //設置連接 cmd.Connection = conn; //設置命令字:創(chuàng)建表 cmd.CommandText = "CREATE TABLE GpsPoints(ID integer,B numeric,L numeric,H numeric)"; //執(zhí)行sql語句,不需要返回值。 cmd.ExecuteNonQuery(); //讀取id列最大值 int next_id = 0; cmd.CommandText = "select max(id) from GpsPoints"; //讀取結果 SQLiteDataReader reader = cmd.ExecuteReader(); //如果沒取到,空數據,則id設置為0 if (reader.IsDBNull(0)) next_id = 0; //反之最大值+1 else next_id = int.Parse(reader[0].ToString()) + 1; //關閉讀取 reader.Close(); //創(chuàng)建命令,插入數據,這里例子沒使用SQLiteParameter cmd.CommandText = "insert into GpsPoints values(" + next_id.ToString() + ",123.456,123.456,123.456)"; cmd.ExecuteNonQuery(); cmd.CommandText = "select max(id) from GpsPoints"; reader = cmd.ExecuteReader(); MessageBox.Show(reader[0].ToString()); conn.Close(); }
|