在本演练中,将从 SQL Server 数据库检索数据,并在 DataGrid 控件中显示该数据。 您可以使用 ADO.NET Entity Framework 创建代表数据的实体类,使用 LINQ 编写从实体类中检索指定数据的查询。
系统必备
您需要以下组件来完成本演练:
Visual Studio 2010.
对附加了 AdventureWorksLT2008 示例数据库的 SQL Server 或 SQL Server Express 的正在运行的实例的访问权限。 您可以从 CodePlex 网站下载 AdventureWorksLT2008 数据库。
创建实体类
使用 Visual Basic 或 C# 创建一个新的 WPF 应用程序项目,并将其命名为 DataGridSQLExample。
在解决方案资源管理器中右击您的项目,指向**“添加”,然后选择“新建项”**。
将显示“添加新项”对话框。
在“已安装的模板”窗格中,选择**“数据”并在模板列表中选择“ADO.NET 实体数据模型”**。
将文件命名为 AdventureWorksModel.edmx,然后单击**“添加”**。
将显示实体数据模型向导。
在“选择模型内容”屏幕上,选择**“从数据库生成”,然后单击“下一步”**。
在“选择您的数据连接”屏幕上,提供与 AdventureWorksLT2008 数据库的连接。 有关更多信息,请参见 Choose Your Data Connection Dialog Box(“选择您的数据连接”对话框)。
确保名称为 AdventureWorksLT2008Entities 且选中**“将 App.Config 中的实体连接设置另存为”复选框,然后单击“下一步”**。
在“选择数据库对象”屏幕中,展开“表”节点,然后选择**“Product”和“ProductCategory”**表。
您可以为所有表生成实体类;但是,在此示例中,只从这两个表检索数据。
单击**“完成”**。
“Product”和“ProductCategory”实体显示在实体设计器中。
检索并显示数据
打开 MainWindow.xaml 文件。
在 XAML 编辑器中,在 <Grid> 和 </Grid> 标记之间添加以下 DataGrid 标记,以添加一个名为 dataGrid1 的 DataGrid。
<DataGrid Name="dataGrid1" />
选择 Window。
使用“属性”窗口或 XAML 编辑器,为 Loaded 事件的名为 Window_Loaded 的 Window 创建一个事件处理程序。 有关更多信息,请参见如何:创建简单的事件处理程序。
下面显示了 MainWindow.xaml 的 XAML。
注意
如果使用的是 Visual Basic,请在 MainWindow.xaml 的第一行中将 x:Class="DataGridSQLExample.MainWindow" 替换为 x:Class="MainWindow"。
<Window x:Class="DataGridSQLExample.MainWindow" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="450" Loaded="Window_Loaded"> <Grid> <DataGrid Name="dataGrid1" /> </Grid> </Window>
打开 Window 的代码隐藏文件(MainWindow.xaml.vb 或 MainWindow.xaml.cs)。
添加下面的代码,以便仅从联接表中检索特定值,并将 DataGrid 的 ItemsSource 属性设置为查询的结果。
Imports System.Data.Objects Class MainWindow Dim dataEntities As AdventureWorksLT2008Entities = New AdventureWorksLT2008Entities Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded Dim products As ObjectQuery(Of Product) = dataEntities.Products Dim query = _ From product In products _ Where product.Color = "Red" _ Order By product.ListPrice _ Select product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice dataGrid1.ItemsSource = query.ToList() End Sub End Class
using System.Data.Objects; using System.Linq; using System.Windows; namespace DataGridSQLExample { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { AdventureWorksLT2008Entities dataEntities = new AdventureWorksLT2008Entities(); public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { ObjectQuery<Product> products = dataEntities.Products; var query = from product in products where product.Color == "Red" orderby product.ListPrice select new { product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice }; dataGrid1.ItemsSource = query.ToList(); } } }
运行该示例。
您应当看到显示数据的 DataGrid。
请参见
参考
其他资源
How Do I: Get Started with Entity Framework in WPF Applications?