轻量级 SQLite数据库帮助类 增删改查 分页

前端之家收集整理的这篇文章主要介绍了轻量级 SQLite数据库帮助类 增删改查 分页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.sqlite; //引用
using System.Data;
using System.Data.Common;

namespace MSCL
{
    /// <summary>   
    /// 本类为sqlite数据库帮助类   
    /// 轻量级数据库sqlite的连接字符串写法:"Data Source=D:\database\test.s3db"   
    /// 轻量级数据库sqlite的加密后的连接字符串写法:"Data Source=Maximus.db;Version=3;Password=myPassword;"  
    /*
         string connStr = "Data Source=" + Server.MapPath("~/App_Data/test.db") + ";Version=3;Password=smile;";        
         DataSet ds = new DataSet();        
         MSCL.sqliteHelper sqlite = new MSCL.sqliteHelper(connStr);        
         ds = sqlite.ExecuteDataSet("Select * From LoginTable",CommandType.Text);
    */
    /// </summary>   
    public class sqliteHelper
    {
        //数据库连接字符串   
        private readonly string _conn = string.Empty;

        public sqliteHelper(string connectionString)
        {
            _conn = connectionString;
        }

        #region ExecuteNonQuery
        /// <summary>   
        /// 执行数据库操作(新增、更新或删除)   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="cmd">sqlCommand对象</param>   
        /// <returns>所受影响的行数</returns>   
        public int ExecuteNonQuery(sqliteCommand cmd)
        {
            int result = 0;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            using (sqliteConnection con = new sqliteConnection(_conn))
            {
                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="connectionString">连接字符串</param>   
        /// <param name="commandText">执行语句或存储过程名</param>   
        /// <param name="commandType">执行类型</param>   
        /// <returns>所受影响的行数</returns>   
        public int ExecuteNonQuery(string commandText,CommandType commandType)
        {
            int result = 0;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            if (string.IsNullOrEmpty(commandText))
                throw new ArgumentNullException("commandText");
            sqliteCommand cmd = new sqliteCommand();
            using (sqliteConnection con = new sqliteConnection(_conn))
            {
                sqliteTransaction trans = null;
                PrepareCommand(cmd,commandType,commandText);
                try
                {
                    result = cmd.ExecuteNonQuery();
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
            }
            return result;
        }

        /// <summary>   
        /// 执行数据库操作(新增、更新或删除)   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="commandText">执行语句或存储过程名</param>   
        /// <param name="commandType">执行类型</param>   
        /// <param name="cmdParms">sql参数对象</param>   
        /// <returns>所受影响的行数</returns>   
        public int ExecuteNonQuery(string commandText,CommandType commandType,params sqliteParameter[] cmdParms)
        {
            int result = 0;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            if (string.IsNullOrEmpty(commandText))
                throw new ArgumentNullException("commandText");

            sqliteCommand cmd = new sqliteCommand();
            using (sqliteConnection con = new sqliteConnection(_conn))
            {
                sqliteTransaction trans = null;
                PrepareCommand(cmd,commandText,cmdParms);
                try
                {
                    result = cmd.ExecuteNonQuery();
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
            }
            return result;
        }
        #endregion

        #region ExecuteScalar
        /// <summary>   
        /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="cmd">sqlCommand对象</param>   
        /// <returns>查询所得的第1行第1列数据</returns>   
        public object ExecuteScalar(sqliteCommand cmd)
        {
            object result = 0;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            using (sqliteConnection con = new sqliteConnection(_conn))
            {
                sqliteTransaction trans = null;
                PrepareCommand(cmd,cmd.CommandText);
                try
                {
                    result = cmd.ExecuteScalar();
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
            }
            return result;
        }

        /// <summary>   
        /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="commandText">执行语句或存储过程名</param>   
        /// <param name="commandType">执行类型</param>   
        /// <returns>查询所得的第1行第1列数据</returns>   
        public object ExecuteScalar(string commandText,CommandType commandType)
        {
            object result = 0;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            if (string.IsNullOrEmpty(commandText))
                throw new ArgumentNullException("commandText");
            sqliteCommand cmd = new sqliteCommand();
            using (sqliteConnection con = new sqliteConnection(_conn))
            {
                sqliteTransaction trans = null;
                PrepareCommand(cmd,commandText);
                try
                {
                    result = cmd.ExecuteScalar();
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
            }
            return result;
        }

        /// <summary>   
        /// 执行数据库操作(新增、更新或删除)同时返回执行后查询所得的第1行第1列数据   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="commandText">执行语句或存储过程名</param>   
        /// <param name="commandType">执行类型</param>   
        /// <param name="cmdParms">sql参数对象</param>   
        /// <returns>查询所得的第1行第1列数据</returns>   
        public object ExecuteScalar(string commandText,params sqliteParameter[] cmdParms)
        {
            object result = 0;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            if (string.IsNullOrEmpty(commandText))
                throw new ArgumentNullException("commandText");

            sqliteCommand cmd = new sqliteCommand();
            using (sqliteConnection con = new sqliteConnection(_conn))
            {
                sqliteTransaction trans = null;
                PrepareCommand(cmd,commandText);
                try
                {
                    result = cmd.ExecuteScalar();
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
            }
            return result;
        }
        #endregion

        #region ExecuteReader
        /// <summary>   
        /// 执行数据库查询,返回sqlDataReader对象   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="cmd">sqlCommand对象</param>   
        /// <returns>sqlDataReader对象</returns>   
        public DbDataReader ExecuteReader(sqliteCommand cmd)
        {
            DbDataReader reader = null;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");

            sqliteConnection con = new sqliteConnection(_conn);
            sqliteTransaction trans = null;
            PrepareCommand(cmd,false,cmd.CommandText);
            try
            {
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return reader;
        }

        /// <summary>   
        /// 执行数据库查询,返回sqlDataReader对象   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="commandText">执行语句或存储过程名</param>   
        /// <param name="commandType">执行类型</param>   
        /// <returns>sqlDataReader对象</returns>   
        public DbDataReader ExecuteReader(string commandText,CommandType commandType)
        {
            DbDataReader reader = null;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            if (string.IsNullOrEmpty(commandText))
                throw new ArgumentNullException("commandText");

            sqliteConnection con = new sqliteConnection(_conn);
            sqliteCommand cmd = new sqliteCommand();
            sqliteTransaction trans = null;
            PrepareCommand(cmd,commandText);
            try
            {
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return reader;
        }

        /// <summary>   
        /// 执行数据库查询,返回sqlDataReader对象   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="commandText">执行语句或存储过程名</param>   
        /// <param name="commandType">执行类型</param>   
        /// <param name="cmdParms">sql参数对象</param>   
        /// <returns>sqlDataReader对象</returns>   
        public DbDataReader ExecuteReader(string commandText,params sqliteParameter[] cmdParms)
        {
            DbDataReader reader = null;
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            if (string.IsNullOrEmpty(commandText))
                throw new ArgumentNullException("commandText");

            sqliteConnection con = new sqliteConnection(_conn);
            sqliteCommand cmd = new sqliteCommand();
            sqliteTransaction trans = null;
            PrepareCommand(cmd,cmdParms);
            try
            {
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return reader;
        }
        #endregion

        #region ExecuteDataSet
        /// <summary>   
        /// 执行数据库查询,返回DataSet对象   
        /// </summary>   
        /// <param name="connectionString">连接字符串</param>   
        /// <param name="cmd">sqlCommand对象</param>   
        /// <returns>DataSet对象</returns>   
        public DataSet ExecuteDataSet(sqliteCommand cmd)
        {
            DataSet ds = new DataSet();
            sqliteConnection con = new sqliteConnection(_conn);
            sqliteTransaction trans = null;
            PrepareCommand(cmd,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="connectionString">连接字符串</param>   
        /// <param name="commandText">执行语句或存储过程名</param>   
        /// <param name="commandType">执行类型</param>   
        /// <returns>DataSet对象</returns>   
        public DataSet ExecuteDataSet(string commandText,CommandType commandType)
        {
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            if (string.IsNullOrEmpty(commandText))
                throw new ArgumentNullException("commandText");
            DataSet ds = new DataSet();
            sqliteConnection con = new sqliteConnection(_conn);
            sqliteCommand cmd = new sqliteCommand();
            sqliteTransaction trans = null;
            PrepareCommand(cmd,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="connectionString">连接字符串</param>   
        /// <param name="commandText">执行语句或存储过程名</param>   
        /// <param name="commandType">执行类型</param>   
        /// <param name="cmdParms">sql参数对象</param>   
        /// <returns>DataSet对象</returns>   
        public DataSet ExecuteDataSet(string commandText,params sqliteParameter[] cmdParms)
        {
            if (string.IsNullOrEmpty(_conn))
                throw new ArgumentNullException("Connection string is missing.");
            if (string.IsNullOrEmpty(commandText))
                throw new ArgumentNullException("commandText");
            DataSet ds = new DataSet();
            sqliteConnection con = null;
            sqliteCommand cmd = new sqliteCommand();
            sqliteTransaction trans = null;
            try
            {
                con = new sqliteConnection(_conn);
                PrepareCommand(cmd,cmdParms);
                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

        /// <summary>   
        /// 通用分页查询方法   
        /// </summary>   
        /// <param name="connString">连接字符串</param>   
        /// <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 DataTable SelectPaging(string tableName,string strColumns,string strWhere,string strOrder,int pageSize,int currentIndex,out int recordOut)
        {
            DataTable dt = new DataTable();
            //查询总数   
            string countsql = "select count(*) from " + tableName + " where {0}";
            countsql = String.Format(countsql,strWhere);
            recordOut = Convert.ToInt32(ExecuteScalar(countsql,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;
        }

        /// <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 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);
            }
        }
    }
}
原文链接:https://www.f2er.com/sqlite/201650.html

猜你在找的Sqlite相关文章