CC# 读取 SQLite 数据库并绑定到表格控件
时间: 2025-07-22 08:25:43 浏览: 16
<think>我们有一个明确的任务:使用C#读取SQLite数据库并将数据绑定到DataGridView控件。根据引用内容,我们可以总结出以下步骤:
1. 添加必要的引用(System.Data.SQLite)和确保SQLite.Interop.dll在运行目录下。
2. 创建SQLite连接,打开数据库。
3. 执行SQL查询(例如SELECT语句)获取数据。
4. 使用SQLiteDataAdapter填充DataTable。
5. 将DataTable设置为DataGridView的数据源。
6. 关闭数据库连接(注意:在填充数据后关闭连接,避免锁定问题)。
同时,我们需要注意引用[2]中提到的“数据库被锁定”问题,因此要确保及时关闭连接,或者使用using语句自动释放资源。
下面我们将按照步骤详细说明,并给出代码示例。</think>### C# 读取 SQLite 数据库并绑定到 DataGridView
以下是实现 SQLite 数据库读取并绑定到 DataGridView 的完整步骤和代码示例:
---
#### **1. 添加 NuGet 包**
首先安装 SQLite 库:
- 在 Visual Studio 中打开 NuGet 包管理器
- 搜索并安装 `System.Data.SQLite`
---
#### **2. 核心代码实现**
```csharp
using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;
public partial class MainForm : Form
{
private SQLiteConnection conn;
private const string dbPath = "Data Source=test.db;Version=3;";
public MainForm()
{
InitializeComponent();
}
// 加载数据到 DataGridView
private void LoadData()
{
try
{
// 创建连接并打开
conn = new SQLiteConnection(dbPath);
conn.Open();
// 查询数据(以 student 表为例)
string sql = "SELECT * FROM student";
SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, conn);
// 填充 DataTable
DataTable dt = new DataTable();
adapter.Fill(dt);
// 绑定到 DataGridView
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show($"错误: {ex.Message}");
}
finally
{
// 确保关闭连接(避免数据库锁定)
if (conn != null && conn.State == ConnectionState.Open)
conn.Close();
}
}
// 窗体加载时调用
private void MainForm_Load(object sender, EventArgs e)
{
LoadData();
}
}
```
---
#### **3. 关键注意事项**
1. **数据库锁定问题**
务必在 `finally` 块中关闭连接(如示例所示),否则会导致其他进程无法访问数据库[^2]。
2. **SQLite.Interop.dll 依赖**
需将 `SQLite.Interop.dll` 复制到输出目录(通常在 `bin\x86` 或 `bin\x64` 中),否则会报错[^4]。
3. **连接字符串说明**
```csharp
// 基础格式
"Data Source=数据库路径;Version=3;"
// 示例(相对路径)
"Data Source=..\\..\\data\\mydb.db;"
```
---
#### **4. 数据绑定优化**
```csharp
// 自动调整列宽
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
// 禁止编辑数据
dataGridView1.ReadOnly = true;
// 美化表头
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.LightBlue;
```
---
#### **5. 常见问题解决**
- **错误:"database is locked"**
检查是否在所有分支路径中都关闭了数据库连接[^2]。
- **错误:"Unable to load DLL SQLite.Interop.dll"**
手动复制 `SQLite.Interop.dll` 到输出目录,或通过 NuGet 安装完整包[^4]。
- **数据不显示**
检查 SQL 语句是否正确,可用 SQLite 工具(如 DB Browser for SQLite)验证查询结果。
---
### 可视化扩展
若需将数据展示为图表(如引用[3]所述),可添加 `System.Windows.Forms.DataVisualization` 引用:
```csharp
// 饼状图示例(假设 chart1 是 Chart 控件)
chart1.Series["Series1"].ChartType = SeriesChartType.Pie;
foreach (DataRow row in ((DataTable)dataGridView1.DataSource).Rows)
{
chart1.Series["Series1"].Points.AddXY(row["name"], row["grade"]);
}
```
---
### 相关问题
1. 如何实现 DataGridView 的增删改功能并同步到 SQLite 数据库?[^5]
2. SQLite 数据库被锁定时有哪些排查方法?
3. 如何优化大数据量(10万+行)在 DataGridView 中的显示性能?
4. C# 如何将 SQLite 查询结果导出为 Excel 文件?
[引用来源]
[^1]: DataGridView 显示 SQLite 数据的基本流程
[^2]: 数据库锁定问题的原因和解决方案
[^3]: 数据可视化实现方法
[^4]: SQLite.Interop.dll 依赖处理
[^5]: DataGridView 数据同步回数据库的机制
阅读全文
相关推荐



















