WYcolorful
To be better me
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();
                }
            }
        }
    }
posted on 2016-07-13 20:45  WYcolorful  阅读(1475)  评论(0编辑  收藏  举报