SqlLite内存数据库

public class MemoryHelper
    {
        /// <summary>
        /// lcok obj
        /// </summary>
        private static readonly object obj = new object();
        /// <summary>
        ///实例
        /// </summary>
        private static MemoryHelper _MemoryHelper { get; set; }
        /// <summary>
        /// SqlConnection
        /// </summary>
        private SQLiteConnection SQLCon { get; set; }
        /// <summary>
        /// Instance
        /// </summary>
        /// <returns></returns>
        public static MemoryHelper Instance()
        {
            lock (obj)
            {
                if (_MemoryHelper == null)
                {
                    lock (obj)
                    {
                        _MemoryHelper = new MemoryHelper();
                    }
                }
            }
            return _MemoryHelper;
        }
        /// <summary>
        /// Construct
        /// </summary>
        private MemoryHelper()
        {
            ///内存数据库
            SQLCon = new SQLiteConnection("Data Source=:memory:;Version=3; Cache Size = 3000;New=True;Pooling=False;Max Pool Size=100;LongNames = 0; Timeout = 1000; NoTXN = 0; SyncPragma = NORMAL; StepAPI = 0");
            SQLCon.Open();
            CreateTableInfo(SQLCon);
        }
        /// <summary>
        /// 创建数据表
        /// </summary>
        /// <param name="con"></param>
        private void CreateTableInfo(SQLiteConnection con)
        {
            string cmd;

            cmd = @"CREATE TABLE table(
	                ID integer NOT NULL PRIMARY KEY AUTOINCREMENT, 	            
	                name nvarchar(50))";
            SQLiteHelper.ExecuteNonQuery(con, CommandType.Text, cmd, null);
        }
        
        /// <summary>
        /// 删除questionInfo表行数据
        /// </summary>     
        /// <returns></returns>
        private void DelQuestionInfo(string strSql )
        {  
            SQLiteHelper.ExecuteNonQuery(SQLCon, CommandType.Text, strSql);
        }
       
        /// <summary>
        /// 查询题目数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable SelQuestionsInfoSql(string sql)
        {
             SQLiteCommand cmd = new SQLiteCommand();
                CommandType cmdType = CommandType.Text;
                PrepareCommand(cmd, null, cmdType, sql, null);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                cmd.Parameters.Clear();
                return ds.Tables[0];
        }
       
        public void UpdateDataTable(string sql)
        {            
           SQLiteHelper.ExecuteNonQuery(SQLCon, CommandType.Text, sql);
           
        }
        /// <summary>
        /// Prepare a command for execution
        /// </summary>
        /// <param name="cmd">SQLiteCommand object</param>       
        /// <param name="trans">SQLiteTransaction object</param>
        /// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
        /// <param name="cmdText">Command text, e.g. Select * from Products</param>
        /// <param name="cmdParms">SQLiteParameters to use in the command</param>
        private void PrepareCommand(SQLiteCommand cmd, SQLiteTransaction trans, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms)
        {
            cmd.Connection = SQLCon;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;


            if (cmdParms != null)
            {
                foreach (SQLiteParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值