SQLite增删改查

前端之家收集整理的这篇文章主要介绍了SQLite增删改查前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文:http://www.51aspx.com/CodeFile/WolfySQLiteDemo,22494/SQLiteHelper.cs.html

dll文件http://pan.baidu.com/s/1dDEGgZR

 public class sqliteHelper
    {
        private static readonly string databaseName = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["databaseName"];
        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <param name="databaseName">数据库文件路径</param>
        public static void CreateDataBase()
        {
            if (!File.Exists(databaseName))
            {
                sqliteConnection.CreateFile(databaseName);
            }
        }
        /// <summary>
        /// 获得连接对象
        /// </summary>
        /// <returns></returns>
        public static sqliteConnection GetsqliteConnection()
        {
            #region 方法sqliteConnectionStringBuilder connStr = new sqliteConnectionStringBuilder();
            connStr.DataSource = databaseName;
            connStr.Password = "123456";
            connStr.Pooling = true;
            return new sqliteConnection(connStr.ToString());
            #endregion
            #region 方法二
            //return new sqliteConnection(string.Format("Data Source={0};password=123456",databaseName));
            #endregion
        }
        /// <summary>
        /// 匹配参数
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="conn"></param>
        /// <param name="cmdText"></param>
        /// <param name="p"></param>
        private static void PrepareCommand(sqliteCommand cmd,sqliteConnection conn,string cmdText,params object[] p)
        {
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
            }
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandTimeout = 30;
            if (p != null)
            {
                foreach (object item in p)
                {
                    cmd.Parameters.AddWithValue(string.Empty,item);
                }
            }
        }
        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataset(string cmdText,params object[] p)
        {
            DataSet ds = new DataSet();
            sqliteCommand cmd = new sqliteCommand();
            using (sqliteConnection conn = GetsqliteConnection())
            {
                PrepareCommand(cmd,conn,cmdText,p);
                sqliteDataAdapter sda = new sqliteDataAdapter(cmd);
                sda.Fill(ds);
            }
            return ds;
        }
        /// <summary>
        /// 返回第一行
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static DataRow ExecuteDataRow(string cmdText,params object[] p)
        {
            DataSet ds = ExecuteDataset(cmdText,p);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 执行非查询操作 返回受影响的行数
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string cmdText,params object[] p)
        {
            sqliteCommand cmd = new sqliteCommand();
            using (sqliteConnection conn = GetsqliteConnection())
            {
                PrepareCommand(cmd,p);
                return cmd.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// 返回sqliteDataReader
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static sqliteDataReader ExecuteReader(string cmdText,params object[] p)
        {
            sqliteConnection conn = GetsqliteConnection();
            sqliteCommand cmd = new sqliteCommand();
            try
            {
                PrepareCommand(cmd,p);
                sqliteDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return reader;
            }
            catch (Exception)
            {
                conn.Close();
                throw;
            }
        }
        /// <summary>
        /// 返回结果集的首行首列
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string cmdText,p);
                return cmd.ExecuteScalar();
            }
        }
        /// <summary>
        /// 分页获取DataSet
        /// </summary>
        /// <param name="recordCount"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="cmdText"></param>
        /// <param name="countText"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static DataSet ExecutePager(ref int recordCount,int pageIndex,int pageSize,string countText,params object[] p)
        {
            if (recordCount < 0)
                recordCount = int.Parse(ExecuteScalar(countText,p).ToString());
            DataSet ds = new DataSet();
            sqliteCommand command = new sqliteCommand();
            using (sqliteConnection connection = GetsqliteConnection())
            {
                PrepareCommand(command,connection,p);
                sqliteDataAdapter da = new sqliteDataAdapter(command);
                da.Fill(ds,(pageIndex - 1) * pageSize,pageSize,"result");
            }
            return ds;
        }
    }
原文链接:https://www.f2er.com/sqlite/200715.html

猜你在找的Sqlite相关文章