C#中SQLite数据库的安装及应用
1、SQLite数据库介绍
SQLite是一款轻量级嵌入式开源数据库,数据库存放在磁盘的一个文件中,最大支持2TB数据库大小,代码足够小,大致13万C代码(约4.43M),无服务器,无需配置,操作简单,通过API轻松访问。
2、SQLite数据库安装
打开C#项目,在工具/NuGet包管理器中发开“管理解决方案的NuGet包”对话框中选择“l浏览”,在查询框中输入“SQLite”,在查询结果中选择“System.Data.SQLite”进行安装,安装后在项目的引用文件可一看到System.Data.SQLite、System.Data.SQLite.EF6、System.Data.SQLite.Linq三项。
在这里插入图片描述

3、SQLite数据库基本操作
(1)创建数据表

/// <summary>
        /// 创建数据表
        /// </summary>
        public static void Create()
        {
            string dbFile = @"URI=file:sql.db";
            SQLiteConnection con = new SQLiteConnection(dbFile);
            con.Open();
            string sql = "Create table Employee (Id integer primary key, Name text);";
            SQLiteCommand cmd = new SQLiteCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }

(2)添加数据

/// <summary>
        /// 添加数据
        /// </summary>
        public static void Insert()
        {
            string dbFile = @"URI=file:sql.db";
            SQLiteConnection con = new SQLiteConnection(dbFile);
            con.Open();
            string sql = "Insert into Employee (Id , Name) values(1, '武松');";
            SQLiteCommand cmd = new SQLiteCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("Insert Id=1 Name=武松 OK");
        }

(3)查询数据

/// <summary>
        /// 查询数据
        /// </summary>
        public static void Query()
        {
            string dbFile = @"URI=file:sql.db";
            SQLiteConnection con = new SQLiteConnection(dbFile);
            con.Open();
            string sql = "Select * From Employee;";
            SQLiteCommand cmd = new SQLiteCommand(sql, con);
            SQLiteDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine($"{reader.GetInt32(0)} {reader.GetString(1)}");
            }
            reader.Close();
            con.Close();        
        }

(4)修改数据

/// <summary>
        /// 修改数据
        /// </summary>
        public static void Update()
        {
            string dbFile = @"URI=file:sql.db";
            SQLiteConnection con = new SQLiteConnection(dbFile);
            con.Open();
            string sql = "update Employee set Name='林冲' where Id=1;";
            SQLiteCommand cmd = new SQLiteCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("update Id=1 Name=林冲 OK");
        }

(5)删除数据

/// <summary>
        /// 删除数据
        /// </summary>
        public static void Delete()
        {
            string dbFile = @"URI=file:sql.db";
            SQLiteConnection con = new SQLiteConnection(dbFile);
            con.Open();
            string sql = "delete from Employee where Id=1;";
            SQLiteCommand cmd = new SQLiteCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Console.WriteLine("delete Id=1 OK");
        }

4、宿主程序调用

private void button2_Click(object sender, EventArgs e)
        {
            SQLiteDriver.Create();
            SQLiteDriver.Insert();
            SQLiteDriver.Query();
            SQLiteDriver.Update();
            SQLiteDriver.Query();
            SQLiteDriver.Delete();
            SQLiteDriver.Query();
        }

5、运行结果
在这里插入图片描述
在这里插入图片描述

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐