zoukankan      html  css  js  c++  java
  • sqlite数据库的使用helper

    public class SQLiteHelper
        {
            //public static String ConnectionString = "Data Source=H:/SQLite/DB/minWeiGanBu.db;Pooling=true;FailIfMissing=false";
            //public static String ConnectionString = "Data Source=H:/SQLite/DB/aid/minWeiGanBu.db;Pooling=true;FailIfMissing=false";
            static string ConnectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString.ToString().Replace("|path|",Tools.binPath + @"DB");
     
            private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params SQLiteParameter[] p)
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                cmd.Parameters.Clear();
                cmd.Connection = conn;
                cmd.CommandText = cmdText;
                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = 30;
                if (p != null)
                {
                    foreach (object parm in p)
                        //cmd.Parameters.AddWithValue(string.Empty, parm);
                        cmd.Parameters.Add(parm);
                }
            }
            public static DataSet ExecuteQuery(string cmdText, params SQLiteParameter[] p)
            {
                using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
                {
                    using (SQLiteCommand command = new SQLiteCommand())
                    {
                        DataSet ds = new DataSet();
                        PrepareCommand(command, conn, cmdText, p);
                        SQLiteDataAdapter da = new SQLiteDataAdapter(command);
                        da.Fill(ds);
                        return ds;
                    }
                }
            }
            public static int ExecuteNonQuery(string cmdText, params SQLiteParameter[] p)
            {
                using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
                {
                    using (SQLiteCommand command = new SQLiteCommand())
                    {
                        PrepareCommand(command, conn, cmdText, p);
                        return command.ExecuteNonQuery();
                    }
                }
            }
     
            public static SQLiteDataReader ExecuteReader(string cmdText, params SQLiteParameter[] p)
            {
                //using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
                //{
                   SQLiteConnection conn = new SQLiteConnection(ConnectionString);
                    using (SQLiteCommand command = new SQLiteCommand())
                    {
                        PrepareCommand(command, conn, cmdText, p);
                        return command.ExecuteReader(CommandBehavior.CloseConnection);
                    }
                //}         
            }
     
            public static object ExecuteScalar(string cmdText, params SQLiteParameter[] p)
            {
                using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
                {
                    using (SQLiteCommand command = new SQLiteCommand())
                    {
                        PrepareCommand(command, conn, cmdText, p);
                        return command.ExecuteScalar();
                    }
                }
            }
        }
  • 相关阅读:
    第一讲小结(位运算)
    新学期开始
    前缀表达式-怎样用空格分隔一个字符串
    计概期末前的小小总结
    动规作业-求数组不相邻元素之和的最大值
    网格-递归作业2-放苹果问题
    1025 数的划分(搜索和递推方法)
    网格-递归作业 集合里的乘法
    网格-递归作业 城堡问题
    去先导零的一个大坑
  • 原文地址:https://www.cnblogs.com/wygm/p/5668166.html
Copyright © 2011-2022 走看看