C# winform 读取sqlite
时间: 2023-11-08 18:06:38 浏览: 57
在 C# WinForms 中读取 SQLite 数据库,你可以使用 System.Data.SQLite 库来实现。下面是一个简单的示例代码:
首先,确保你已经安装了 System.Data.SQLite NuGet 包。然后,在你的代码文件中添加以下 using 声明:
```csharp
using System.Data.SQLite;
```
接下来,你可以使用以下代码来读取 SQLite 数据库中的数据:
```csharp
string connectionString = "Data Source=path_to_your_database_file.db;Version=3;";
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
connection.Open();
string query = "SELECT * FROM your_table_name";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// 读取数据并进行处理
string column1Value = reader.GetString(0);
int column2Value = reader.GetInt32(1);
// 在这里执行你的逻辑
}
}
}
}
```
请将 `path_to_your_database_file.db` 替换为你实际的数据库文件路径,将 `your_table_name` 替换为你要查询的表名。你可以根据需要修改查询语句和处理逻辑。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)