C#里面SQLite读取数据的操作
生活随笔
收集整理的這篇文章主要介紹了
C#里面SQLite读取数据的操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//掛載表格時候用
public static DataSet Query(string SQLString){using (SQLiteConnection connection = new SQLiteConnection(connectionString)){DataSet ds = new DataSet();try{connection.Open();SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);command.Fill(ds, "ds");}catch (System.Data.SQLite.SQLiteException ex){throw new Exception(ex.Message);}return ds; //返回的是一個DataSet}}
/// 執(zhí)行查詢語句,返回SQLiteDataReader//獲取返回值用/// </summary>/// <param name="strSQL">查詢語句</param>/// <returns>SQLiteDataReader</returns>public static SQLiteDataReader ExecuteReader(string strSQL){SQLiteConnection connection = new SQLiteConnection(connectionString);SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);try{connection.Open();SQLiteDataReader myReader = cmd.ExecuteReader();return myReader;}catch (System.Data.SQLite.SQLiteException e){throw new Exception(e.Message);}}
應(yīng)用:
掛載表格的時候直接用
Grd.DataSource=Query(SQL).Tables[0]
/// 執(zhí)行查詢語句,返回SQLiteDataReader//獲取返回值用/// </summary>/// <param name="strSQL">查詢語句</param>/// <returns>SQLiteDataReader</returns>public static SQLiteDataReader ExecuteReader(string strSQL){SQLiteConnection connection = new SQLiteConnection(connectionString);SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);try{connection.Open();SQLiteDataReader myReader = cmd.ExecuteReader();return myReader;}catch (System.Data.SQLite.SQLiteException e){throw new Exception(e.Message);}}
應(yīng)用
SQLiteDataReader SqlDr = DbSQLite.ExecuteReader(sql); //SQLiteDataReader需要先引用,using System.Data.SQLite;try { while(SqlDr.Read()){int rn = SqlDr.GetInt32(1);string rname=SqlDr.GetValue(0).ToString(); //字段讀取方式if (rn == 5){//if (rname.Equals("1"))//{// str += SqlDr.GetValue(2).ToString() + "是" + " ";//}//else {// str += SqlDr.GetValue(2).ToString() + "否" + " ";//}}}catch{ }總結(jié)
以上是生活随笔為你收集整理的C#里面SQLite读取数据的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决:bash: vim: comman
- 下一篇: 解决: cp -rf 命令中参数 -f