C# 连接 Debug 目录下的 SQLite 数据库
在 C# 中连接目标文件 Debug 目录内的 SQLite 数据库,可以使用以下步骤:
-
使用 System.Data.SQLite 命名空间,首先需要在项目中添加 SQLite DLL 引用。
-
创建连接字符串,指定 SQLite 数据库文件路径。
-
创建 SQLite 连接对象,使用连接字符串初始化连接对象。
-
打开连接对象。
-
执行 SQL 语句,获取数据。
-
关闭连接对象。
下面是示例代码:
using System.Data.SQLite;
string connectionString = 'Data Source=Debug\mydatabase.db;Version=3;';
SQLiteConnection connection = new SQLiteConnection(connectionString);
connection.Open();
//执行SQL语句
SQLiteCommand command = new SQLiteCommand('SELECT * FROM mytable', connection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//获取数据
int id = reader.GetInt32(0);
string name = reader.GetString(1);
//...
}
connection.Close();
需要注意的是,连接字符串中指定的数据源路径应该是相对于程序运行目录的路径,而不是项目的路径。因此,在使用 Debug 目录中的 SQLite 数据库时,应该使用相对路径'Debug\mydatabase.db'。
原文地址: https://www.cveoy.top/t/topic/oZNT 著作权归作者所有。请勿转载和采集!