基于C#+SQLite开发数据库应用的示例

SQLite数据库,小巧但功能强大;并且是基于文件型的数据库,驱动库就是一个dll文件,有些开发工具

甚至不需要带这个dll,比如用Delphi开发,用一些三方组件;数据库也是一个文件,虽然是个文件,但却

具有关系型数据库的大多数特征,查询语句也是长得基本一样,所以对应学习数据库操作很方便。

比起前微软的Access数据库那只能说,SQLite强大的太多。

在 Visual Studio 中,可以通过以下步骤安装:

打开 Visual Studio,点击 "工具" -> "NuGet 包管理器" -> "管理解决方案的 NuGet 包" - 在搜索框中输入 "SQLite",然后安装 "System.Data.SQLite" 包。

打开VS,新建一个.NET项目,选择 C# Windows 桌面,Windows窗体应用(.NET Framework)

界面上拖放相应控件

关键事件代码 如下

cs 复制代码
        private void button8_Click(object sender, EventArgs e)
        {

            string dataSource = "test.db"; // 数据库文件名
            // 连接数据库
            using (SQLiteConnection connection = new SQLiteConnection($"Data Source={dataSource};Version=3;"))
            {
                connection.Open(); // 打开连接

                // 创建表
                string createTableQuery = "CREATE TABLE IF NOT EXISTS Customers (Id INTEGER PRIMARY KEY, Name TEXT, Age INT,Phone TEXT)";
                using (SQLiteCommand createTableCommand = new SQLiteCommand(createTableQuery, connection))
                {
                    createTableCommand.ExecuteNonQuery(); // 执行创建表的SQL语句
                }

                // 插入数据
                string insertDataQuery = "INSERT INTO Customers (Name, Age, Phone) VALUES (@Name, @Age, @Phone)";
                using (SQLiteCommand insertDataCommand = new SQLiteCommand(insertDataQuery, connection))
                {
                    insertDataCommand.Parameters.AddWithValue("@Name", "John"); // 设置参数值,避免SQL注入
                    insertDataCommand.Parameters.AddWithValue("@Age", 25);
                    insertDataCommand.Parameters.AddWithValue("@Phone", "123456");
                    insertDataCommand.ExecuteNonQuery(); // 执行插入数据的SQL语句
                }

                // 查询数据
                string selectDataQuery = "SELECT * FROM Customers";
                using (SQLiteCommand selectDataCommand = new SQLiteCommand(selectDataQuery, connection))
                {
                    using (SQLiteDataReader reader = selectDataCommand.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            int id = Convert.ToInt32(reader["Id"]); 
                            string name = Convert.ToString(reader["Name"]);
                            int age = Convert.ToInt32(reader["Age"]);
                            string phone = Convert.ToString(reader["Phone"]);
                            Console.WriteLine($"ID: {id}, Name: {name}, Age: {age}, Phone: {phone}");
                        }
                    }
                }

                SQLiteCommand sqlCommand = new SQLiteCommand("select * from Customers", connection);
                sqlCommand.ExecuteNonQuery();

                DataTable dataTable = new DataTable("Customers");
                SQLiteDataAdapter sqlAdapter = new SQLiteDataAdapter(sqlCommand);
                sqlAdapter.Fill(dataTable);

                dataGridView1.DataSource = dataTable.DefaultView;
                sqlAdapter.Update(dataTable);
            }
        }

程序运行结果

相关推荐
白帽小丑35 分钟前
# 一次 MySQL DELETE 误操作的数据恢复尝试实录
数据库·mysql
贪玩的蛋挞2 小时前
C#与闭包
开发语言·c#
Quincy_Freak3 小时前
信创内网数据规范实践:银河麒麟下SQLite本地数据安全管理方案
数据库·sqlite·arm·数据库管理·大数据分析·银河麒麟·aarch64
2601_962683895 小时前
治理遗留系统中的“生肉 SQL”:一次用多模型协作优化慢查询的实战复盘
数据库·人工智能·sql
酱学编程5 小时前
【从零到一实现一个 AI Agent 框架 · 第四篇】04. 任务规划:拆解复杂目标 -
服务器·网络·数据库·人工智能
shushangyun_7 小时前
2026智能采购商城系统选型指南:如何引领企业数字化采购升级
java·大数据·数据库·人工智能·机器学习
忧郁的紫菜7 小时前
基础实现:单篇 Markdown 转 Word
开发语言·c#·word
甜美的小天鹅7 小时前
Swifter C#之inline还是不inline,这是个问题
开发语言·c#
华山令狐虫8 小时前
DBAPI AI 写 SQL:支持动态 SQL 与参数占位符,自然语言一键生成
数据库·人工智能·sql·dbapi
IvorySQL8 小时前
PG 技术日报|2026-07-04
数据库·人工智能·postgresql·开源