WPF--->使用SqLite操作db文件

SqLite通过NuGet包的基础使用

基础安装

在WPF中使用SQLite,先安装System.Data.SqLite.Core包
创建类进行操作,示例如下

封装操作db文件基础类

创建对*.db文件的操作类,封装基本的操作,db文件通过手动创建或者工具创建都可以

  public class MySqLite
    {
        // 数据库文件夹
        static string DbPath = Path.Combine(@"./", @"Database");

        //与指定的数据库(实际上就是一个文件)建立连接
        private static SQLiteConnection CreateDatabaseConnection(string dbName = null)
        {
            if (!string.IsNullOrEmpty(DbPath) && !Directory.Exists(DbPath))
                Directory.CreateDirectory(DbPath);
            dbName = dbName == null ? "database.db" : dbName;
            var dbFilePath = Path.Combine(DbPath, dbName);
            return new SQLiteConnection("DataSource = " + dbFilePath);
        }

        // 使用全局静态变量保存连接
        private static SQLiteConnection connection = CreateDatabaseConnection();

        // 判断连接是否处于打开状态
        private static void Open(SQLiteConnection connection)
        {
            if (connection.State != System.Data.ConnectionState.Open)
            {
                connection.Open();
            }
        }

        //执行非查询SQL语句代码,适用于建表、增删改等
        public static void ExecuteNonQuery(string sql)
        {
            // 确保连接打开
            Open(connection);

            using (var tr = connection.BeginTransaction())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = sql;
                    command.ExecuteNonQuery();
                }
                tr.Commit();
            }
        }

        public static void ExecuteQuery(string sql)
        {
            // 确保连接打开
            Open(connection);

            using (var tr = connection.BeginTransaction())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = sql;

                    // 执行查询会返回一个SQLiteDataReader对象
                    var reader = command.ExecuteReader();

                    //reader.Read()方法会从读出一行匹配的数据到reader中。注意:是一行数据。
                    while (reader.Read())
                    {
                        // 有一系列的Get方法,方法的参数是列数。意思是获取第n列的数据,转成Type返回。
                        // 比如这里的语句,意思就是:获取第0列的数据,转成int值返回。
                        var time = reader.GetInt64(0);
                    }
                }
                tr.Commit();
            }
        }

        // 因为SQLite是文件型数据库,可以直接删除文件。但只要数据库连接没有被回收,就无法删除文件。
        public static void DeleteDatabase(string dbName)
        {
            var path = Path.Combine(DbPath, dbName);
            connection.Close();

            // 置空,手动GC,并等待GC完成后执行文件删除。
            connection = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            File.Delete(path);
        }

进行使用

        static void Main(string[] args)
        {
            MySqLite.ExecuteNonQuery("create table if not exists table1(id int, name varchar(30),age int)");
            MySqLite.ExecuteNonQuery(" insert into table2 values(2,'TOM',23)");
           Console.Read();
        }

SqLite

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值