標籤:blog os io 資料 ar div cti log
做C#朋友的一個擷取DataSet函數,對C#不熟,整理整理,瞭解怎麼用
//掛載表格時候用
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 } }
應用:
掛載表格的時候直接用
Grd.DataSource=Query(SQL).Tables[0]
/// 執行查詢語句,返回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); } }
應用
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() + "是" + "\r\n"; //} //else { // str += SqlDr.GetValue(2).ToString() + "否" + "\r\n"; //} } }catch { }