SQLite增删改查

SQLite增删改查,第1张

概述原文: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

原文: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;        }    }
总结

以上是内存溢出为你收集整理的SQLite增删改查全部内容,希望文章能够帮你解决SQLite增删改查所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1172792.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存