程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 用於sqlite數據庫的SqlLiteHelper.cs 的類

用於sqlite數據庫的SqlLiteHelper.cs 的類

編輯:C#入門知識

 

 using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

www.2cto.com

using System.Data; 

using System.Data.SQLite; 

using System.Data.Common; 

 

namespace JonseTest 

   public abstract class SqlLiteHelper 

    { 

       public static string ConnSqlLiteDbPath = string.Empty; 

       public static string ConnString 

       { 

           get 

           { 

               return string.Format(@"Data Source={0}", ConnSqlLiteDbPath); 

           } 

       } 

 

       // 取datatable 

       public static DataTable GetDataTable(out string sError,string sSQL) 

       { 

           DataTable dt = null; 

           sError = string.Empty; 

 

           try 

           { 

               SQLiteConnection conn = new SQLiteConnection(ConnString); 

               conn.Open(); 

               SQLiteCommand cmd = new SQLiteCommand(); 

               cmd.CommandText = sSQL; 

               cmd.Connection = conn; 

               SQLiteDataAdapter dao = new SQLiteDataAdapter(cmd); 

               dt = new DataTable(); 

               dao.Fill(dt); 

           } 

           catch (Exception ex) 

           { 

               sError = ex.Message; 

           } 

 

           return dt; 

       } 

 

       // 取dataset 

       public static DataSet GetDataSet(out string sError, string sSQL) 

       { 

           DataSet ds = null; 

           sError = string.Empty; 

 

           try 

           { 

               SQLiteConnection conn = new SQLiteConnection(ConnString); 

               conn.Open(); 

               SQLiteCommand cmd = new SQLiteCommand(); 

               cmd.CommandText = sSQL; 

               cmd.Connection = conn; 

               SQLiteDataAdapter dao = new SQLiteDataAdapter(cmd); 

               ds = new DataSet(); 

               dao.Fill(ds); 

           } 

           catch (Exception ex) 

           { 

               sError = ex.Message; 

           } 

 

           return ds; 

       } 

 

       // 取某個單一的元素 

       public static object GetSingle(out string sError, string sSQL) 

       { 

           DataTable dt = GetDataTable(out sError, sSQL); 

           if (dt != null && dt.Rows.Count > 0) 

           { 

               return dt.Rows[0][0]; 

           } 

 

           return null; 

       } 

 

       // 取最大的ID 

       public static Int32 GetMaxID(out string sError, string sKeyField,string sTableName) 

       { 

           DataTable dt = GetDataTable(out sError, "select ifnull(max([" + sKeyField + "]),0) as MaxID from [" + sTableName + "]"); 

           if (dt != null && dt.Rows.Count > 0) 

           { 

               return Convert.ToInt32(dt.Rows[0][0].ToString()); 

           } 

 

           return 0; 

       } 

 

       // 執行insert,update,delete 動作,也可以使用事務 

       public static bool UpdateData(out string sError, string sSQL,bool bUseTransaction=false) 

       { 

           int iResult = 0; 

           sError = string.Empty; 

 

           if (!bUseTransaction) 

           { 

               try 

               { 

                   SQLiteConnection conn = new SQLiteConnection(ConnString); 

                   conn.Open(); 

                   SQLiteCommand comm = new SQLiteCommand(conn); 

                   comm.CommandText = sSQL; 

                   iResult = comm.ExecuteNonQuery(); 

               } 

               catch (Exception ex) 

               { 

                   sError = ex.Message; 

                   iResult = -1; 

               } 

           } 

           else // 使用事務 

           { 

               DbTransaction trans =null; 

               try 

               { 

                   SQLiteConnection conn = new SQLiteConnection(ConnString); 

                   conn.Open(); 

                   trans = conn.BeginTransaction(); 

                   SQLiteCommand comm = new SQLiteCommand(conn); 

                   comm.CommandText = sSQL; 

                   iResult = comm.ExecuteNonQuery(); 

                   trans.Commit(); 

               } 

               catch (Exception ex) 

               { 

                   sError = ex.Message; 

                   iResult = -1; 

                   trans.Rollback(); 

               } 

           } 

 

           return iResult >0; 

       } 

 

    } 

 

  

 

 

 

關於具體如何調用SqlLiteHelper ,請參考我的博文:

 

C# 使用sqlite 輕量級數據庫

 

網址:http://www.BkJia.com/kf/201111/111179.html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved