| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- /**************************************
- * 作用:SQLLite Server操作实现
- * 作者:Nick.Yan
- * 日期: 2009-03-29
- * 网址:www.redglove.com.cn
- * 源码:http://www.51aspx.com/CV/SQLLiteDemo
- **************************************/
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Data;
- using System.Data.SQLite;
- using System.Configuration;
- using System.Collections.Generic;
- namespace SiteCore
- {
- public class SQLiteHelper
- {
- public static string connectionString = "";//Data Source="+ webConfig.sqllite+"; UTF8Encoding=true
- //public static string connectionString = "Data Source=" + Application.StartupPath + "/SqlFile/Csbq.s3db;UTF8Encoding=true;";
- public SQLiteHelper() { }
- #region 公用方法
- public static int GetMaxID(string FieldName, string TableName)
- {
- string strsql = "select max(" + FieldName + ")+1 from " + TableName;
- object obj = GetSingle(strsql);
- if (obj == null)
- {
- return 1;
- }
- else
- {
- return int.Parse(obj.ToString());
- }
- }
- public static bool Exists(string strSql)
- {
- object obj = GetSingle(strSql);
- int cmdresult;
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- cmdresult = 0;
- }
- else
- {
- cmdresult = int.Parse(obj.ToString());
- }
- if (cmdresult == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public static bool Exists(string strSql, params SQLiteParameter[] cmdParms)
- {
- object obj = GetSingle(strSql, cmdParms);
- int cmdresult;
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- cmdresult = 0;
- }
- else
- {
- cmdresult = int.Parse(obj.ToString());
- }
- if (cmdresult == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- #endregion
- #region 执行简单SQL语句
- /// <summary>
- /// 执行SQL语句,返回影响的记录数
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <returns>影响的记录数</returns>
- public static int TestExecuteSql(string SQLString)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- connection.Close();
- throw new Exception(E.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行SQL语句,返回影响的记录数
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <returns>影响的记录数</returns>
- public static int ExecuteSql(string SQLString)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
- {
- connection.Open();
- SQLiteTransaction myTrans = connection.BeginTransaction(IsolationLevel.Serializable);
- try
- {
- cmd.Transaction = myTrans;
- int rows = cmd.ExecuteNonQuery();
- myTrans.Commit();
- return rows;
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- myTrans.Rollback();
- connection.Close();
- throw new Exception(E.Message);
- }
- }
- }
- }
- public static int ExecuteSql(string SQLString,string connString)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connString))
- {
- using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
- {
- connection.Open();
- SQLiteTransaction myTrans = connection.BeginTransaction(IsolationLevel.Serializable);
- try
- {
- cmd.Transaction = myTrans;
- int rows = cmd.ExecuteNonQuery();
- myTrans.Commit();
- return rows;
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- myTrans.Rollback();
- connection.Close();
- throw new Exception(E.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行SQL语句,设置命令的执行等待时间
- /// </summary>
- /// <param name="SQLString"></param>
- /// <param name="Times"></param>
- /// <returns></returns>
- public static int ExecuteSqlByTime(string SQLString, int Times)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- cmd.CommandTimeout = Times;
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- connection.Close();
- throw new Exception(E.Message);
- }
- }
- }
- }
- public static void ExecuteSqlTran(List<string> SQLStringList)
- {
- using (SQLiteConnection conn = new SQLiteConnection(connectionString))
- {
- conn.Open();
- SQLiteCommand cmd = new SQLiteCommand();
- cmd.Connection = conn;
- SQLiteTransaction tx = conn.BeginTransaction();
- cmd.Transaction = tx;
- try
- {
- for (int n = 0; n < SQLStringList.Count; n++)
- {
- string strsql = SQLStringList[n].ToString();
- if (strsql.Trim().Length > 1)
- {
- cmd.CommandText = strsql;
- cmd.ExecuteNonQuery();
- }
- }
- tx.Commit();
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- tx.Rollback();
- throw new Exception(E.Message);
- }
- finally
- {
- cmd.Dispose();
- conn.Close();
- }
- }
- }
- /// <summary>
- /// 执行多条SQL语句,实现数据库事务。
- /// </summary>
- /// <param name="SQLStringList">多条SQL语句</param>
- public static void ExecuteSqlTran(ArrayList SQLStringList)
- {
- using (SQLiteConnection conn = new SQLiteConnection(connectionString))
- {
- conn.Open();
- SQLiteCommand cmd = new SQLiteCommand();
- cmd.Connection = conn;
- SQLiteTransaction tx = conn.BeginTransaction();
- cmd.Transaction = tx;
- try
- {
- for (int n = 0; n < SQLStringList.Count; n++)
- {
- string strsql = SQLStringList[n].ToString();
- if (strsql.Trim().Length > 1)
- {
- cmd.CommandText = strsql;
- cmd.ExecuteNonQuery();
- }
- }
- tx.Commit();
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- tx.Rollback();
- throw new Exception(E.Message);
- }
- finally
- {
- cmd.Dispose();
- conn.Close();
- }
- }
- }
- /// <summary>
- /// 执行带一个存储过程参数的的SQL语句。
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
- /// <returns>影响的记录数</returns>
- //public static int ExecuteSql(string SQLString, string content)
- //{
- // using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- // {
- // SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
- // SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
- // myParameter.Value = content;
- // cmd.Parameters.Add(myParameter);
- // try
- // {
- // connection.Open();
- // int rows = cmd.ExecuteNonQuery();
- // return rows;
- // }
- // catch (System.Data.SQLite.SQLiteException E)
- // {
- // throw new Exception(E.Message);
- // }
- // finally
- // {
- // cmd.Dispose();
- // connection.Close();
- // }
- // }
- //}
- /// <summary>
- /// 执行带一个存储过程参数的的SQL语句。
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
- /// <returns>影响的记录数</returns>
- public static object ExecuteSqlGet(string SQLString, string content)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
- SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
- myParameter.Value = content;
- cmd.Parameters.Add(myParameter);
- try
- {
- connection.Open();
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- throw new Exception(E.Message);//发送出错的信息至服务器
- }
- finally
- {
- cmd.Dispose();
- connection.Close();
- }
- }
- }
- /// <summary>
- /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
- /// </summary>
- /// <param name="strSQL">SQL语句</param>
- /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
- /// <returns>影响的记录数</returns>
- public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
- SQLiteParameter myParameter = new SQLiteParameter("@fs", DbType.Binary);
- myParameter.Value = fs;
- cmd.Parameters.Add(myParameter);
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- throw new Exception(E.Message);
- }
- finally
- {
- cmd.Dispose();
- connection.Close();
- }
- }
- }
- /// <summary>
- /// 执行一条计算查询结果语句,返回查询结果(object)。
- /// </summary>
- /// <param name="SQLString">计算查询结果语句</param>
- /// <returns>查询结果(object)</returns>
- public static object GetSingle(string SQLString)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
- {
- try
- {
- connection.Open();
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (System.Data.SQLite.SQLiteException e)
- {
- connection.Close();
- throw new Exception(e.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行查询语句,返回SQLiteDataReader(使用该方法切记要手工关闭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);//发送出错的信息至服务器
- }
- //finally //不能在此关闭,否则,返回的对象将无法使用
- //{
- // cmd.Dispose();
- // connection.Close();
- //}
- }
- /// <summary>
- /// 执行查询语句,返回DataSet
- /// </summary>
- /// <param name="SQLString">查询语句</param>
- /// <returns>DataSet</returns>
- 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)
- {
- }
- return ds;
- }
- }
- public static DataSet Query(string SQLString, string TableName)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- DataSet ds = new DataSet();
- try
- {
- connection.Open();
- SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
- command.Fill(ds, TableName);
- }
- catch (System.Data.SQLite.SQLiteException ex)
- {
- }
- return ds;
- }
- }
- /// <summary>
- /// 执行查询语句,返回DataSet,设置命令的执行等待时间
- /// </summary>
- /// <param name="SQLString"></param>
- /// <param name="Times"></param>
- /// <returns></returns>
- public static DataSet Query(string SQLString, int Times)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- DataSet ds = new DataSet();
- try
- {
- connection.Open();
- SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
- command.SelectCommand.CommandTimeout = Times;
- command.Fill(ds, "ds");
- }
- catch (System.Data.SQLite.SQLiteException ex)
- {
- }
- return ds;
- }
- }
- #endregion
- #region 执行带参数的SQL语句
- /// <summary>
- /// 执行SQL语句,返回影响的记录数
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <returns>影响的记录数</returns>
- public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- using (SQLiteCommand cmd = new SQLiteCommand())
- {
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- int rows = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- return rows;
- }
- catch (System.Data.SQLite.SQLiteException E)
- {
- throw new Exception(E.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行多条SQL语句,实现数据库事务。
- /// </summary>
- /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SQLiteParameter[])</param>
- public static void ExecuteSqlTran(Hashtable SQLStringList)
- {
- using (SQLiteConnection conn = new SQLiteConnection(connectionString))
- {
- conn.Open();
- using (SQLiteTransaction trans = conn.BeginTransaction())
- {
- SQLiteCommand cmd = new SQLiteCommand();
- try
- {
- //循环
- foreach (DictionaryEntry myDE in SQLStringList)
- {
- string cmdText = myDE.Key.ToString();
- SQLiteParameter[] cmdParms = (SQLiteParameter[])myDE.Value;
- PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- trans.Commit();
- }
- }
- catch
- {
- trans.Rollback();
- throw;
- }
- }
- }
- }
- /// <summary>
- /// 执行一条计算查询结果语句,返回查询结果(object)。
- /// </summary>
- /// <param name="SQLString">计算查询结果语句</param>
- /// <returns>查询结果(object)</returns>
- public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- using (SQLiteCommand cmd = new SQLiteCommand())
- {
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- object obj = cmd.ExecuteScalar();
- cmd.Parameters.Clear();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (System.Data.SQLite.SQLiteException e)
- {
- throw new Exception(e.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行查询语句,返回SQLiteDataReader (使用该方法切记要手工关闭SQLiteDataReader和连接)
- /// </summary>
- /// <param name="strSQL">查询语句</param>
- /// <returns>SQLiteDataReader</returns>
- public static SQLiteDataReader ExecuteReader(string SQLString, params SQLiteParameter[] cmdParms)
- {
- SQLiteConnection connection = new SQLiteConnection(connectionString);
- SQLiteCommand cmd = new SQLiteCommand();
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- SQLiteDataReader myReader = cmd.ExecuteReader();
- cmd.Parameters.Clear();
- return myReader;
- }
- catch (System.Data.SQLite.SQLiteException e)
- {
- throw new Exception(e.Message);//发送出错的信息至服务器
- }
- //finally //不能在此关闭,否则,返回的对象将无法使用
- //{
- // cmd.Dispose();
- // connection.Close();
- //}
- }
- /// <summary>
- /// 执行查询语句,返回DataSet
- /// </summary>
- /// <param name="SQLString">查询语句</param>
- /// <returns>DataSet</returns>
- public static DataSet Query(string SQLString, params SQLiteParameter[] cmdParms)
- {
- using (SQLiteConnection connection = new SQLiteConnection(connectionString))
- {
- SQLiteCommand cmd = new SQLiteCommand();
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- try
- {
- da.Fill(ds, "ds");
- cmd.Parameters.Clear();
- }
- catch (System.Data.SQLite.SQLiteException ex)
- {
- throw new Exception(ex.Message);//发送出错的信息至服务器
- }
- return ds;
- }
- }
- }
- public static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, SQLiteParameter[] cmdParms)
- {
- if (conn.State != ConnectionState.Open)
- conn.Open();
- cmd.Connection = conn;
- cmd.CommandText = cmdText;
- if (trans != null)
- cmd.Transaction = trans;
- cmd.CommandType = CommandType.Text;//cmdType;
- if (cmdParms != null)
- {
- foreach (SQLiteParameter parameter in cmdParms)
- {
- if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
- (parameter.Value == null))
- {
- parameter.Value = DBNull.Value;
- }
- cmd.Parameters.Add(parameter);
- }
- }
- }
- #endregion
- #region 参数转换
- /// <summary>
- /// 放回一个SQLiteParameter
- /// </summary>
- /// <param name="name">参数名字</param>
- /// <param name="type">参数类型</param>
- /// <param name="size">参数大小</param>
- /// <param name="value">参数值</param>
- /// <returns>SQLiteParameter的值</returns>
- public static SQLiteParameter MakeSQLiteParameter(string name, DbType type, int size, object value)
- {
- SQLiteParameter parm = new SQLiteParameter(name, type, size);
- parm.Value = value;
- return parm;
- }
- public static SQLiteParameter MakeSQLiteParameter(string name, DbType type, object value)
- {
- SQLiteParameter parm = new SQLiteParameter(name, type);
- parm.Value = value;
- return parm;
- }
- #endregion
- }
- }
|