码迷,mamicode.com
首页 > 数据库 > 详细

sqlite数据库的使用helper

时间:2016-07-14 01:22:43      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:

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();
                }
            }
        }
    }

sqlite数据库的使用helper

标签:

原文地址:http://www.cnblogs.com/wygm/p/5668166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!