演练:在 DataGrid 控件中显示 SQL Server 数据库中的数据
演示如何从 SQL Server 数据库检索数据并在 DataGrid 控件中显示该数据。 可以使用 ADO.NET Entity Framework 创建表示数据的实体类,并使用 LINQ 编写查询,以便从实体类检索指定数据。
先决条件
你需要满足以下条件才能完成本演练:
Visual Studio。
可访问附加了 AdventureWorksLT 示例数据库的 SQL Server 或 SQL Server Express 的正在运行的实例。 可以从 GitHub 下载 AdventureWorks 数据库。
创建实体类
在 Visual Basic 或 C# 中创建一个新的 WPF 应用程序项目,并将其命名为
DataGridSQLExample
。在解决方案资源管理器中,右键单击项目,指向“添加”,然后选择“新建项目”。
“添加新项” 对话框随即出现。
在“已安装的模板”窗格中,选择“数据”,然后在模板列表中,选择“ADO.NET 实体数据模型”。
将文件命名为
AdventureWorksModel.edmx
,然后单击“添加”。此时将显示实体数据模型向导。
在“选择模型内容”屏幕中,选择“数据库中的 EF 设计器”,然后单击“下一步”。
在“选择数据连接”屏幕中,提供与 AdventureWorksLT2008 数据库的连接。 有关详细信息,请参阅选择“数据连接”对话框。
确保名称为
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="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://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 属性设置为查询结果。
using System.Data.Entity.Core.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) { var query = from product in dataEntities.Products where product.Color == "Red" orderby product.ListPrice select new { product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice }; dataGrid1.ItemsSource = query.ToList(); } } }
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
运行示例。
应该会看到一个显示数据的 DataGrid。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈