SQLite版的C#的dbhelper类

作者: Nishizhen 分类: C#相关 发布时间: 2010-02-09 15:18 ė 6没有评论

程序用SQLite做配置存储很方便,自己改写的dbhelper

HOHO~


  public class DBHelper
    {
        /// <summary>
        /// 
        /// </summary>
        private static SQLiteConnection connection;

        public static SQLiteConnection Connection
        {
            get
            {
                //本地数据库的数据库连接字符串。
                string connectionString = "data source=config.db;Password=XXXXXX;";

                if (connection == null)
                {
                    connection = new SQLiteConnection(connectionString);
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;
            }
        }

        public static int ExecuteCommand(string safeSql)
        {
            SQLiteCommand cmd = new SQLiteCommand(safeSql, Connection);
            int result = cmd.ExecuteNonQuery();
            return result;
        }

        public static int ExecuteCommand(string sql, params SQLiteParameter[] values)
        {
            SQLiteCommand cmd = new SQLiteCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteNonQuery();
        }

        public static int GetScalar(string safeSql)
        {
            SQLiteCommand cmd = new SQLiteCommand(safeSql, Connection);
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            return result;
        }

        public static int GetScalar(params SQLiteParameter[] values)
        {
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = Connection;
            cmd.CommandText = "Pro_InsertOrder";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            return result;
        }

        public static int GetScalar(string sql, params SQLiteParameter[] values)
        {
            SQLiteCommand cmd = new SQLiteCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            return result;
        }

        public static SQLiteDataReader GetReader(string safeSql)
        {
            SQLiteCommand cmd = new SQLiteCommand(safeSql, Connection);
            SQLiteDataReader reader = cmd.ExecuteReader();
            return reader;
        }

        public static SQLiteDataReader GetReader(string sql, params SQLiteParameter[] values)
        {
            SQLiteCommand cmd = new SQLiteCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            SQLiteDataReader reader = cmd.ExecuteReader();
            return reader;
        }

        public static DataTable GetDataSet(string safeSql)
        {
            DataSet ds = new DataSet();
            SQLiteCommand cmd = new SQLiteCommand(safeSql, Connection);
            SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

        public static DataTable GetDataSet(string sql, params SQLiteParameter[] values)
        {
            DataSet ds = new DataSet();
            SQLiteCommand cmd = new SQLiteCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }
    }

本文出自 逆时针's Place,转载时请注明出处及相应链接。

本文永久链接: http://www.nishizhen.cn/sqlite%e7%89%88%e7%9a%84c%e7%9a%84dbhelper%e7%b1%bb/

发表评论

电子邮件地址不会被公开。 必填项已用*标注

验证码 * Time limit is exhausted. Please reload CAPTCHA.

Ɣ回顶部