如何将DataGridView绑定到SQLite数据库?

问题描述 投票:0回答:3

我正在尝试向使用 SQLite 的 datagridview 添加数据连接。我已经添加了对 SQLite 的引用(下载了所需的文件),但是当我通过向导添加数据源时,SQLite 未包含在选项中 - 请参阅下面的屏幕截图:

我四处寻找这个问题的答案,但没有成功找到任何答案。

这是我的项目文件夹中数据库的屏幕截图:

有什么方法可以将datagridview绑定到sqlite数据源吗?

如有任何帮助,我们将不胜感激。

谢谢!

c# .net sqlite datagridview
3个回答
9
投票

以下步骤复制自http://geekswithblogs.net/danielggarcia/archive/2013/12/22/portable-databases-ii-using-sqlite-with-entity-framework.aspx:

  1. 转到“工具”>“库包管理器”>“管理 NuGet 包”以获取解决方案...
  2. 选择 Online 部分并在右上角的文本框中写入 SQLite。按 ENTER 并等待执行搜索。
  3. 选择第一个包:System.Data.SQLite (x86/x64)。
  4. 下载软件包后,选择要安装提供程序的项目,然后按“确定”。

如果按照上述步骤不起作用后数据源仍然不可用,则可能是 SQLite 版本的问题 - 请参阅cmc 留下的注释链接的线程,其中显示版本 1.0 .93 可以工作,但 1.0.94 版本不行。


2
投票
  1. 转到“工具”>“库包管理器”>“管理 NuGet 包”以获取解决方案...
  2. 选择“浏览”部分并搜索 Microsoft.SqlServer.Compact
  3. 安装它
  4. 单击查看 > 其他窗口 > SQLite/SQL Server Compact Toolbox 将出现 出现在那里。
  5. 点击SQLite/SQL Server Compact Toolbox即可轻松管理 联系。附上截图


0
投票

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); }
    
© www.soinside.com 2019 - 2024. All rights reserved.