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

 

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

写这篇文章是因为一位坑逼同事,整天问我怎么操作数据库,不就是增删改查吗,有什么困难吗?我个人觉得他是因为懒。算了就当是丰富笔记了,顺便解决一天的文章更新。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);
            }

 

posted @ 2021-07-22 08:35  Cmale  阅读(5636)  评论(1编辑  收藏  举报