C#與SQLite的操作介紹

來源:互聯網
上載者:User

SQLite ADO .NET:

http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

 

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沒多大區別了。

 

//建立一個資料庫檔案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(’dotnetthink’,'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())...{    sb.Append("username:").Append(reader.GetString(0)).Append("\n")    .Append("password:").Append(reader.GetString(1));}MessageBox.Show(sb.ToString());
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.