C# SQLite操作 特別注意事項

最近整理資料發現SQLite數據雖然Android有用過,但是C#還沒整理成庫,索性馬上抽時間研究一番。


使用庫:System.Data.SQLite.dll(實際聽說還有一種sqlite3.dll也不錯的樣子下次有空再玩這個庫)

庫下載地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

幾句比較重要點。


1:連接字符串->

Basic(基本的)
     Data Source=filename;Version=3;
Using UTF16(使用UTF16編碼)
    Data Source=filename;Version=3;UseUTF16Encoding=True;
With password(帶密碼的)
    Data Source=filename;Version=3;Password=myPassword;
Using the pre 3.3x database format(使用3.3x前數據庫格式)
     Data Source=filename;Version=3;Legacy Format=True;
Read only connection(只讀連接)
    Data Source=filename;Version=3;Read Only=True;
With connection pooling(設置連接池)
    Data Source=filename;Version=3;Pooling=False;Max Pool Size=100;
Using DateTime.Ticks as datetime format()
     Data Source=filename;Version=3;DateTimeFormat=Ticks;

2:獲取當前DB的所有表,視圖,索引等。

 "select name from sqlite_master where type='table'";

 "select name from sqlite_master where type='view'";

 "select name from sqlite_master where type='index'"

3:獲取某個表的字段詳細信息。(這裏比較坑,因爲並沒有特別好用的Sql語句可以直接實現,所以採用ADO通過以下代碼方式獲取DataSet)

SQLiteConnection conn = new SQLiteConnection(connectionString);
conn.Open();
DataTable schemaTable = null;
IDbCommand cmd = new SQLiteCommand();
cmd.CommandText = string.Format("select * from [{0}]", tableName);
cmd.Connection = conn ;
 
using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
{
  schemaTable = reader.GetSchemaTable();
}
conn.Close()
return schemaTable;
4:最最最重要的一點就是,sql語句一定要注意全部轉成小寫,別問爲什麼,對於這個System.Data.SQLite.dll庫就是這麼玩的,否則會出現無法預期的結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章