积累——SQLiteHelper

来源:互联网 发布:cmd java 命令行参数 编辑:程序博客网 时间:2024/05/21 00:50
using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Data.SQLite;using System.Linq;using System.Text;using System.Security.Cryptography;using System.IO;using sasacModel;namespace sasacCommon{    public class SqliteHelper    {        public static string StringKey = ConfigurationManager.AppSettings["StringKey"].ToString();//加密秘钥        private static readonly string strSqliteConn = strSqliteConns();        private static string strSqliteConns()        {            common com = new common();            string db = ConfigurationManager.AppSettings["sqliteDB"].ToString();            string strSqliteConn = @"Data Source=" + db;            return strSqliteConn;        }        /// <summary>        /// 由用户指定sql语句,可以不传SQLiteParameter的参数,但是不能传null        /// </summary>        /// <param name="strCmd"></param>        /// <param name="ps"></param>        /// <returns></returns>        public static int ExecuteNonQuery(string strCmd, params SQLiteParameter[] ps)        {            try            {                using (SQLiteConnection SqliteConn = new SQLiteConnection(strSqliteConn))                {                    using (SQLiteCommand SqliteCmd = new SQLiteCommand(strCmd, SqliteConn))                    {                        SqliteCmd.Parameters.AddRange(ps);                        SqliteConn.Open();                        return SqliteCmd.ExecuteNonQuery();                    }                }            }            catch (Exception e)            {                throw e;            }                    }        //无参        public static int ExecuteNonQuery(string strCmd)        {            try            {                using (SQLiteConnection SqliteConn = new SQLiteConnection(strSqliteConn))                {                    using (SQLiteCommand SqliteCmd = new SQLiteCommand(strCmd, SqliteConn))                    {                        SqliteConn.Open();                        return SqliteCmd.ExecuteNonQuery();                    }                }            }            catch (Exception e)            {                throw e;            }                    }        public static object ExecuteScalar(string strCmd, params SQLiteParameter[] ps)        {            try            {                using (SQLiteConnection SqliteConn = new SQLiteConnection(strSqliteConn))                {                    using (SQLiteCommand SqliteCmd = new SQLiteCommand(strCmd, SqliteConn))                    {                        SqliteCmd.Parameters.AddRange(ps);                        SqliteConn.Open();                        return SqliteCmd.ExecuteScalar();                    }                }            }            catch (Exception e)            {                throw e;            }                   }        public static SQLiteDataReader ExecuteReader(string strCmd, params SQLiteParameter[] ps)        {            SQLiteConnection SqliteConn = null;            try            {                SqliteConn = new SQLiteConnection(strSqliteConn);                using (SQLiteCommand SqliteCmd = new SQLiteCommand(strCmd, SqliteConn))                {                    SqliteCmd.Parameters.AddRange(ps);                    SqliteConn.Open();                    return SqliteCmd.ExecuteReader(CommandBehavior.CloseConnection);                }            }            catch (Exception e)            {                if (SqliteConn != null)                {                    SqliteConn.Dispose();                }                throw e;            }        }        //无参数        public static SQLiteDataReader ExecuteReader(string strCmd)        {            SQLiteConnection SqliteConn = null;            try            {                SqliteConn = new SQLiteConnection(strSqliteConn);                using (SQLiteCommand SqliteCmd = new SQLiteCommand(strCmd, SqliteConn))                {                    SqliteConn.Open();                    return SqliteCmd.ExecuteReader(CommandBehavior.CloseConnection);                }            }            catch (Exception e)            {                if (SqliteConn != null)                {                    SqliteConn.Dispose();                }                throw e;            }        }        public static DataTable ExecuteTable(string strCmd, params SQLiteParameter[] ps)        {            try            {                using (SQLiteDataAdapter SqliteAdapter = new SQLiteDataAdapter(strCmd, strSqliteConn))                {                    SqliteAdapter.SelectCommand.Parameters.AddRange(ps);                    DataTable table = new DataTable();                    SqliteAdapter.Fill(table);                    return table;                }            }            catch (Exception e)            {                                throw e;            }                   }        //无参数        public static DataTable ExecuteTable(string strCmd)        {            try            {                using (SQLiteDataAdapter SqliteAdapter = new SQLiteDataAdapter(strCmd, strSqliteConn))                {                    DataTable table = new DataTable();                    SqliteAdapter.Fill(table);                    return table;                }            }            catch (Exception e)            {                                throw e;            }                   }           }public enum ExecuteMode{    ExecuteNonQuery,    ExecuteScalar,    ExecuteReader}


原创粉丝点击