C#使用Sqlite3

本文详细介绍了如何在C#中使用SQLite3数据库,包括封装SqliteHelper类以进行数据库操作,判断SQLite中是否存在特定表格,以及演示了基本的增、删、查功能的实现调用方式。
摘要由CSDN通过智能技术生成

1.封装SqliteHelper类

 public class SqliteHelper
    {
        #region Fields
        /// <summary>
        /// 连接字符串
        /// </summary>
        private static string connStr = "Data Source={0};Version=3;BinaryGUID=false;";
        /// <summary>
        /// 密码
        /// </summary>
        private static string pwd = "@Ta&nSu7855qdg$%o12!34#56!7/.89!";
       /// <summary>
       /// 创建表命令
       /// </summary>
        private static string cmdCreateTable = "CREATE TABLE IF NOT EXISTS OutPutInStorageInfoLog (ID integer NOT NULL PRIMARY KEY,Number text,GoodsType text, CreateTime TEXT,Status INT);";
        private static object lockObj = new object();
        #endregion

        #region Connection
        /// <summary>
        /// 连接(DB文件不存在,则会自动创建)
        /// </summary>
        /// <param name="filePath">本地db文件路径</param>
        public static void Connection(string filePath)
        {
            connStr = string.Format(connStr, filePath);
            if (!File.Exists(filePath))
            {
                SQLiteConnection.CreateFile(filePath);
                using (SQLiteConnection connection = new SQLiteConnection(connStr))
                {
                    if (connection.State != System.Data.ConnectionState.Open)
                        connection.Open();
                    connection.ChangePassword(pwd);
                    List<TransModel> models = new List<TransModel>();
                    models.Add(new TransModel { CmdText = cmdCreateTable });
                    ExecTransaction(models);//一个事务:如果表不存在则创建
                }
            }
        }
        #endregion

        #region ExecTransaction
        /// <summary>
        /// 执行事务,如果出现异常则回滚
        /// </summary>
        private static bool ExecTransaction(List<TransModel> models)
        {
            using (SQLiteConnection con = new SQLiteConnection(connStr))
            {
                try
                {
                    con.SetPassword(pwd);//解密
                    if (con.State != System.Data.ConnectionState.Open)
                        con.Open();
                    using (SQLi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxy644492473

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值