C#操作Sqlite轻量级数据库实现增删改查

互联网/新科技 beaji 2022-01-11 13:49 475 0

写这篇文章是因为一位坑逼同事,整天问我怎么操作数据库,不就是增删改查吗,有什么困难吗?我个人觉得他是因为懒。算了就当是丰富笔记了,顺便解决一天的文章更新。C#操作Sqlite数据库很简单,因为Sqlite提供了C#的支持库,只需要引入dll动态链接库,我们就能像操作mysql一样操作Sqlite。下面是C#操作Sqlite轻量级数据库实现增删改查的全过程,一起学习下吧。

Sqlite下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

可视化工具下载地址:https://sqlitestudio.pl/index.rvt?act=download

虽然下载下来是EXE可执行文件,但是我们只需要里面的System.Data.SQLite.dll文件而已,安装过程只是解压,放心安装。

解压好后,通过VS引入里面的System.Data.SQLite.dll文件,然后在你的项目中使用using System.Data.SQLite进行引用。

C#操作Sqlite轻量级数据库实现增删改查

C#操作Sqlite轻量级数据库实现增删改查

C#操作Sqlite

首先声明全局变量:

SQLiteConnection Conn;

创建数据库

string FilePath = Application.StartupPath + "\\" + textBox1.Text + ".db";
            if (!File.Exists(FilePath))
            {
                SQLiteConnection.CreateFile(FilePath);
            }
            try
            {
                Conn = new SQLiteConnection("Data Source=" + FilePath + ";Version=3;");
                Conn.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("打开数据库:" + FilePath + "的连接失败:" + ex.Message);
            }

textBox1是数据库名称。



创建数据表

try
            {
                string sql = "create table " + textBox2.Text + " (name varchar(20), score int)";
                SQLiteCommand command = new SQLiteCommand(sql, Conn);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("创建数据表" + textBox2.Text + "失败:" + ex.Message);
            }

textBox2是数据表名称,Conn是数据库连接,前面设置的全局变量。


增加数据

 try
            {
                string sql = "insert into " + textBox2.Text + " (name, score) values ('" + textBox3.Text + "', " + Convert.ToInt32(textBox4.Text) + ")";
                SQLiteCommand command = new SQLiteCommand(sql, Conn);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("插入数据:" + textBox3.Text + ":" + textBox4.Text + "失败:" + ex.Message);
            }

这里的增加数据与数据表的数据结构需要一一对应,不懂的自己去学习下sql语句。


删除数据

try
            {
                string sql = "delete from " + textBox2.Text + " where " + textBox6.Text + "='" + textBox7.Text + "'";
                SQLiteCommand command = new SQLiteCommand(sql, Conn);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("删除数据:" + textBox6.Text + ":" + textBox7.Text + "失败:" + ex.Message);
            }

同样是使用数据库连接,进行sql查询。


修改数据

try
            {
                string sql = "update " + textBox2.Text + " set score = " + Convert.ToInt32(textBox9.Text) + " where name='" + textBox8.Text + "'";
                SQLiteCommand command = new SQLiteCommand(sql, Conn);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("更新数据:" + textBox8.Text + ":" + textBox9.Text + "失败:" + ex.Message);
            }

查询数据

 try
            {
                string sql = "select * from " + textBox2.Text + " order by score desc";
                SQLiteCommand command = new SQLiteCommand(sql, Conn);
                SQLiteDataReader reader = command.ExecuteReader();
                while (reader.Read()){
                  textBox5.Text = "Name: " + reader["name"] + "\tScore: " + reader["score"] + "\r\n" + textBox5.Text;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("查询数据失败:" + ex.Message);
            }




五大“核芯”产品,一大“技术输出”: 【智慧集团管控平台】: 关键词:【集团专属系统平台】,【实时对比】各区域工厂【同一时间段的生产数据】。 【智能工厂管家】:(MES) 关键词:【现场监控可视化、业务流程规范化、生产管理精细化、能源利用合理化、资源配置最优化、调度决策科学化】。 【智能调度器】:(APC) 关键词:【智能控制】,【关键参数实时优化推荐】,【长期稳定运行】 【孪生工厂】: 关键词:【工厂实体建模】,【设备预警】,【关键参数】、【实时展示大屏】 【超低排放助手】: 关键词:【超低排放】,同时降低【20%】以上的还原剂使用量。 【一大技术输出】  技术成品输出:  关键词:提供【成品软件】,或【专项定制研发软件】。  技术能力输出:  关键词:【专业技术团队】、【驻扎项目】

评论区