• 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();
                    }
                }
            }
        }
  • 相关阅读:
    HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)
    HDU 1006 Tick and Tick(时钟,分钟,秒钟角度问题)
    hdu 2099 整除的尾数
    hdu 2098 分拆素数和(一个偶数拆分成两个不同素数和 拆法数量)
    旅游电车(cogs 1175)
    校长的收藏(洛谷 U4534)
    HXY烧情侣(洛谷 2194)
    矩形面积求并(codevs 3044)
    楼房(洛谷 1382)
    选择数字(codevs 3327)
  • 原文地址:https://www.cnblogs.com/wygm/p/5668166.html
Copyright © 2020-2023  润新知