WPF 和 SQLite 数据库






3.92/5 (9投票s)
WPF 中的主从绑定到 SQLite 数据库

引言
在阅读了大量关于 SQLite 的好文章后,我决定建立一个简单的 WPF 项目,该项目将处理两个 DataGrid
之间的主从关系,并在后台使用 SQLite 数据库。 这是一篇入门文章,因此我不会深入研究数据库层、LINQ 等。
要求
要构建文章项目,您需要以下内容:
- Visual Studio 2010
- SQLite.NET
- SQLite Northwind 数据库
- 如果您使用的是 .NET 3.5,请下载 WPF 工具包
安装用于 SQLite 的 ADO.NET 2.0 提供程序,并确保安装 SQLite Designer(包含在该软件包中); 这样,您就可以直接在 VS 2010 环境中管理数据库。 解压并复制 SQLite Northwind 数据库,最好复制到您的项目文件夹中。
入门
假设您已成功安装上述软件包,创建一个 WPF 项目,根据您的需要命名,并在您的主窗口 (MainWindow.xaml) 上“放置”两个 DataGrid
。 此时,我并不关心应用程序的样式,而是专注于它的功能。
在继续之前,将 DataGrid
的 AutoGenerateColumns
属性设置为 true
; 这将自动为每个字段生成一个列。 对于这种情况,它非常有用; 但是,如果您计划创建一个成熟的应用程序,这种方法无法很好地控制生成的列。
开始享受乐趣吧! 在“数据源”面板上,单击“添加新数据源”。 选择“数据库”,然后选择“数据集”,最后创建“新建连接”。 将数据更改为“SQLite 数据库文件”,浏览该文件,最后选中“测试连接”。 如果一切正常,您应该已成功连接。 下一步是创建“NorthwindDataSet
”,我们稍后将引用它。

WPF C# 代码隐藏
让我们向我们的应用程序添加一些代码。 在我们的示例中,我们将有两个表“Customers
”和“Orders
”。 每次客户端单击“Customer
” DataGrid
时,详细信息 DataGrid
“Orders
” 将自动更新。
将以下代码输入到您的 MainWindow.xaml.cs 中
NorthwindDataSet ds;
// Private var
public MainWindow()
{
InitializeComponent();
//Create new DataSeta
ds = new NorthwindDataSet();
//Create table adapter for Customers
NorthwindDataSetTableAdapters.CustomersTableAdapter customerAdapter =
new NorthwindDataSetTableAdapters.CustomersTableAdapter();
//Populate adapter
customerAdapter.Fill(ds.Customers);
//Bind adapter to datagrid
dataGrid1.ItemsSource = ds.Customers.DefaultView;
//Populate the second datagrid "Orders"
NorthwindDataSetTableAdapters.OrdersTableAdapter orderAdapter =
new NorthwindDataSetTableAdapters.OrdersTableAdapter();
orderAdapter.Fill(ds.Orders);
dataGrid2.ItemsSource = ds.Orders;
}
接下来,双击第一个 datagrid
“Customers
”。 应该创建方法“dataGrid1_SelectionChanged
”,这意味着每次更改选择时,都会调用此方法。 我必须承认,我一直在寻找一种在 WPF 中在 DataGrid
的控件之间创建主从关系的简单方法,但无济于事。 所以我想出了以下代码
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Get selected Row
DataRowView row = (DataRowView) dataGrid1.SelectedItem;
//Get selected Row Cell base on which the datagrid will be changed
string customerId = row.Row["CustomerID"].ToString();
//Check if everything is OK
if (customerId == null || customerId == string.Empty)
{
return ;
}
//Change view based on RowFilet
DataView view = ds.Orders.DefaultView;
view.RowFilter = string.Format("CustomerID='{0}'", customerId);
}
这个想法是在“Customers
”Datagrid
中找到选定的“CustomerID
”,然后根据该选择过滤“Orders
”Datagrid
。 我认为上面的代码是不言自明的。 完整的代码如下所示
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
namespace SQLite_demo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
NorthwindDataSet ds;
public MainWindow()
{
InitializeComponent();
//Create new DataSeta
ds = new NorthwindDataSet();
//Create table adapter for Customers
NorthwindDataSetTableAdapters.CustomersTableAdapter customerAdapter =
new NorthwindDataSetTableAdapters.CustomersTableAdapter();
//Populate adapter
customerAdapter.Fill(ds.Customers);
//Bind adapter to datagrid
dataGrid1.ItemsSource = ds.Customers.DefaultView;
//Populate the second datagrid "Orders"
NorthwindDataSetTableAdapters.OrdersTableAdapter orderAdapter =
new NorthwindDataSetTableAdapters.OrdersTableAdapter();
orderAdapter.Fill(ds.Orders);
dataGrid2.ItemsSource = ds.Orders;
}
private void dataGrid1_SelectionChanged
(object sender, SelectionChangedEventArgs e)
{
//Get selected Row
DataRowView row = (DataRowView) dataGrid1.SelectedItem;
//Get selected Row Cell base on which the datagrid will be changed
string customerId = row.Row["CustomerID"].ToString();
//Check if everything is OK
if (customerId == null || customerId == string.Empty)
{
return ;
}
//Change view based on RowFilet
DataView view = ds.Orders.DefaultView;
view.RowFilter = string.Format("CustomerID='{0}'", customerId);
}
}
}
附加信息
如果您正在针对 .??? 4 Framework 进行开发(您应该这样做),则需要将以下行添加到您的“app.config”中。
<configuration>
<configsections>
</configsections>
<startup uselegacyv2runtimeactivationpolicy="true">
<supportedruntime version="v4.0">
</supportedruntime></startup>
</configuration>
这样,您将避免 .NET 4.0 和 .NET 2.0 之间令人讨厌的兼容性错误。 发生这种情况是因为用于 SQLite 的 ADO.NET 2.0 提供程序是针对版本 .NET 2.0 构建的。 另一种解决方案是下载源代码并针对 .NET 4.0 重新编译它,我从未尝试过,所以完全取决于您。
结论
这是使用 WPF 的 SQLite 数据库的基本介绍。 有了这两个,您可以为您的用户提供使用非 Microsoft 数据库的替代方案,并具有相同甚至更高的性能。
历史
- 2011 年 2 月 3 日:首次发布