我正在尝试向使用 SQLite 的 datagridview 添加数据连接。我已经添加了对 SQLite 的引用(下载了所需的文件),但是当我通过向导添加数据源时,SQLite 未包含在选项中 - 请参阅下面的屏幕截图:
我四处寻找这个问题的答案,但没有成功找到任何答案。
这是我的项目文件夹中数据库的屏幕截图:
有什么方法可以将datagridview绑定到sqlite数据源吗?
如有任何帮助,我们将不胜感激。
谢谢!
System.Data.SQLite
的网站现在表示,其“设计时组件”(我相信它为 Visual Studio UI 中的“数据源”功能提供了动力)现在不再“官方支持”,并且无法在 Visual 中工作Studio 2017 及更高版本“由于 Visual Studio 包安装模型的更改”:https://system.data.sqlite.org/index.html/doc/trunk/www/news.wiki .
有一个单独的第 3 方“SQLite 和 SQL Server Compact Toolbox”Visual Studio 扩展,它提供了数据库浏览器 UI 窗口和 SQLite 数据源,但我无法让后者自行工作(在 VS2019 中)。
最后我只是自己在代码中手动进行了绑定。SQLite*
中的
System.Data.SQLite
类捕捉到本标准 Winforms DataGridView 绑定教程中显示的相同类型的
.DataSource
内容:https://learn.microsoft.com/en-us/dotnet/desktop/winforms/控件/如何将数据绑定到 windows-forms-datagridview-control?view=netframeworkdesktop-4.8
using System.Data.SQLite;
var dbConn = new SQLiteConnection("Data Source=" + pathToDbFile);
var dataAdapter = new SQLiteDataAdapter(
"SELECT * from [Table1]",
dbConn
);
// These can be constructed manually or created in the Designer
DataGridView dataGridView1;
BindingSource bindingSource1;
// This binding can be done manually in constructor/on Load event, or also done via properties in the Designer
dataGridView1.DataSource = bindingSource1;
// Now, on form load or other event, the .Fill method of SQLiteDataAdapter
// can be used just like the System.Data.SqlClient.SqlDataAdapter class
// in the tutorial
{
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable
{
Locale = CultureInfo.InvariantCulture
};
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}