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库就是这么玩的,否则会出现无法预期的结果

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