码迷,mamicode.com
首页 > 数据库 > 详细

Sqlite 帮助类 SQLiteHelper

时间:2015-06-24 23:49:28      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

    ///源码下载地址:http://download.csdn.net/detail/kehaigang29/8836171   
/// <summary> /// 本类为SQLite数据库帮助静态类,使用时只需直接调用即可,无需实例化 /// </summary> public static class SQLiteHelper { /// <summary> /// 数据库连接字符串 /// </summary> public static string connectionString = "Data Source=" + Application.StartupPath + "\\" + System.Configuration.ConfigurationSettings.AppSettings["Contr"]; #region 执行数据库操作(新增、更新或删除),返回影响行数 /// <summary> /// 执行数据库操作(新增、更新或删除) /// </summary> /// <param name="cmd">SqlCommand对象</param> /// <returns>所受影响的行数</returns> public static int ExecuteNonQuery(SQLiteCommand cmd) { int result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText); try { result = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 执行数据库操作(新增、更新或删除) /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <returns>所受影响的行数</returns> public static int ExecuteNonQuery(string commandText, CommandType commandType = CommandType.Text) { int result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, commandType, commandText); try { result = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 执行数据库操作(新增、更新或删除) /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <param name="cmdParms">SQL参数对象</param> /// <returns>所受影响的行数</returns> public static int ExecuteNonQuery(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms) { int result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, commandType, commandText,cmdParms); try { result = cmd.ExecuteNonQuery(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } #endregion #region 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据 /// <summary> /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据 /// </summary> /// <param name="cmd">SqlCommand对象</param> /// <returns>查询所得的第1行第1列数据</returns> public static object ExecuteScalar(SQLiteCommand cmd) { object result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, cmd.CommandType, cmd.CommandText); try { result = cmd.ExecuteScalar(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据 /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <returns>查询所得的第1行第1列数据</returns> public static object ExecuteScalar(string commandText, CommandType commandType = CommandType.Text) { object result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, commandType, commandText); try { result = cmd.ExecuteScalar(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } /// <summary> /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据 /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <param name="cmdParms">SQL参数对象</param> /// <returns>查询所得的第1行第1列数据</returns> public static object ExecuteScalar(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms) { object result = 0; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection con = new SQLiteConnection(connectionString)) { SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, true, commandType, commandText, cmdParms); try { result = cmd.ExecuteScalar(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } return result; } #endregion #region 执行数据库查询,返回SqlDataReader对象 /// <summary> /// 执行数据库查询,返回SqlDataReader对象 /// </summary> /// <param name="cmd">SqlCommand对象</param> /// <returns>SqlDataReader对象</returns> public static DbDataReader ExecuteReader(SQLiteCommand cmd) { DbDataReader reader = null; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, cmd.CommandType, cmd.CommandText); try { reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception ex) { throw ex; } return reader; } /// <summary> /// 执行数据库查询,返回SqlDataReader对象 /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <returns>SqlDataReader对象</returns> public static DbDataReader ExecuteReader(string commandText, CommandType commandType = CommandType.Text) { DbDataReader reader = null; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText); try { reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception ex) { throw ex; } return reader; } /// <summary> /// 执行数据库查询,返回SqlDataReader对象 /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <param name="cmdParms">SQL参数对象</param> /// <returns>SqlDataReader对象</returns> public static DbDataReader ExecuteReader(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms) { DbDataReader reader = null; if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText, cmdParms); try { reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception ex) { throw ex; } return reader; } #endregion #region 执行数据库查询,返回DataSet对象 /// <summary> /// 执行数据库查询,返回DataSet对象 /// </summary> /// <param name="cmd">SqlCommand对象</param> /// <returns>DataSet对象</returns> public static DataSet ExecuteDataSet(SQLiteCommand cmd) { DataSet ds = new DataSet(); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, cmd.CommandType, cmd.CommandText); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(ds); } catch (Exception ex) { throw ex; } finally { if (cmd.Connection != null) { if (cmd.Connection.State == ConnectionState.Open) { cmd.Connection.Close(); } } } return ds; } /// <summary> /// 执行数据库查询,返回DataSet对象 /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <returns>DataSet对象</returns> public static DataSet ExecuteDataSet(string commandText, CommandType commandType = CommandType.Text) { if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); DataSet ds = new DataSet(); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(ds); } catch (Exception ex) { throw ex; } finally { if (con != null) { if (con.State == ConnectionState.Open) { con.Close(); } } } return ds; } /// <summary> /// 执行数据库查询,返回DataSet对象 /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <param name="cmdParms">SQL参数对象</param> /// <returns>DataSet对象</returns> public static DataSet ExecuteDataSet(string commandText, CommandType commandType = CommandType.Text, params SQLiteParameter[] cmdParms) { if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); DataSet ds = new DataSet(); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText, cmdParms); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(ds); } catch (Exception ex) { throw ex; } finally { if (con != null) { if (con.State == ConnectionState.Open) { con.Close(); } } } return ds; } #endregion #region 执行数据库查询,返回DataTable对象 /// <summary> /// 执行数据库查询,返回DataTable对象 /// </summary> /// <param name="commandText">执行语句或存储过程名</param> /// <param name="commandType">执行类型(默认语句)</param> /// <returns>DataTable对象</returns> public static DataTable ExecuteDataTable(string commandText, CommandType commandType = CommandType.Text) { if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText"); DataTable dt = new DataTable(); SQLiteConnection con = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); SQLiteTransaction trans = null; PrepareCommand(cmd, con, ref trans, false, commandType, commandText); try { SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); sda.Fill(dt); } catch (Exception ex) { throw ex; } finally { if (con != null) { if (con.State == ConnectionState.Open) { con.Close(); } } } return dt; } #endregion #region 通用分页查询方法 /// <summary> /// 通用分页查询方法 /// </summary> /// <param name="tableName">表名</param> /// <param name="strColumns">查询字段名</param> /// <param name="strWhere">where条件</param> /// <param name="strOrder">排序条件</param> /// <param name="pageSize">每页数据数量</param> /// <param name="currentIndex">当前页数</param> /// <param name="recordOut">数据总量</param> /// <returns>DataTable数据表</returns> public static DataTable SelectPaging(string tableName, string strColumns, string strWhere, string strOrder, int pageSize, int currentIndex, out int recordOut) { DataTable dt = new DataTable(); recordOut = Convert.ToInt32(ExecuteScalar("select count(*) from " + tableName, CommandType.Text)); string pagingTemplate = "select {0} from {1} where {2} order by {3} limit {4} offset {5} "; int offsetCount = (currentIndex - 1) * pageSize; string commandText = String.Format(pagingTemplate, strColumns, tableName, strWhere, strOrder, pageSize.ToString(), offsetCount.ToString()); using (DbDataReader reader = ExecuteReader(commandText, CommandType.Text)) { if (reader != null) { dt.Load(reader); } } return dt; } #endregion #region 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化 /// <summary> /// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化 /// </summary> /// <param name="cmd">Command对象</param> /// <param name="conn">Connection对象</param> /// <param name="trans">Transcation对象</param> /// <param name="useTrans">是否使用事务</param> /// <param name="cmdType">SQL字符串执行类型</param> /// <param name="cmdText">SQL Text</param> /// <param name="cmdParms">SQLiteParameters to use in the command</param> private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, ref SQLiteTransaction trans, bool useTrans, CommandType cmdType, string cmdText, params SQLiteParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (useTrans) { trans = conn.BeginTransaction(IsolationLevel.ReadCommitted); cmd.Transaction = trans; } cmd.CommandType = cmdType; if (cmdParms != null) { foreach (SQLiteParameter parm in cmdParms) cmd.Parameters.Add(parm); } } #endregion }

 

Sqlite 帮助类 SQLiteHelper

标签:

原文地址:http://www.cnblogs.com/ManchesterUnitedFootballClub/p/4598839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!