C# 连接目标文件 Debug 内的 SQLite 数据库
在 C# 中连接目标文件 Debug 内的 SQLite 数据库,可以按照以下步骤进行操作:
- 引入 System.Data.SQLite 命名空间:
using System.Data.SQLite;
- 创建 SQLite 连接对象:
SQLiteConnection conn = new SQLiteConnection('Data Source=|DataDirectory|\mydb.db;Version=3;');
其中,'Data Source' 指定了数据库文件的路径,这里使用 '|DataDirectory|' 特殊字符串代替了 Debug 目录路径,保证了在不同计算机上的可移植性。
- 打开连接:
conn.Open();
- 执行 SQL 语句:
SQLiteCommand command = new SQLiteCommand('SELECT * FROM mytable', conn);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// 处理查询结果
string name = reader.GetString(0);
int age = reader.GetInt32(1);
// ...
}
- 关闭连接:
conn.Close();
以上就是连接目标文件 Debug 内的 SQLite 数据库的方法。需要注意的是,SQLite 数据库文件必须先被添加到项目中,并设置其 '生成操作' 属性为 '内容',才能在 Debug 目录中找到。
原文地址: https://www.cveoy.top/t/topic/oZNJ 著作权归作者所有。请勿转载和采集!