当前位置:数据库 > SQLite >>

用于sqlite数据库的SqlLiteHelper.cs 的类

 

 using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

www.zzzyk.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 bUseTran

补充:软件开发 , C# ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,