演练:在 DataGrid 控件中显示 SQL Server 数据库中的数据

在本演练中,将从 SQL Server 数据库检索数据,并在 DataGrid 控件中显示该数据。 使用 ADO.NET Entity Framework 创建表示数据的实体类,并使用 LINQ 编写从实体类检索指定数据的查询。

先决条件

需要以下组件才能完成本演练:

  • Visual Studio。

  • 访问 SQL Server 或 SQL Server Express 正在运行的实例,该实例附加了 AdventureWorks 示例数据库。 可以从 GitHub下载 AdventureWorks 数据库。

创建实体类

  1. 在 Visual Basic 或 C# 中创建新的 WPF 应用程序项目,并将其命名为 DataGridSQLExample

  2. 在解决方案资源管理器中,右键单击项目,指向“添加”,然后选择“新建项目”

    此时会显示“添加新项”对话框。

  3. 在“已安装的模板”窗格中,选择 数据,然后在模板列表中选择 ADO.NET 实体数据模型

    ADO.NET 实体数据模型项模板

  4. 将文件命名为 AdventureWorksModel.edmx,然后单击 添加

    此时将显示实体数据模型向导。

  5. 在“选择模型内容”屏幕中,选择“数据库中的 EF 设计器”,然后单击“下一步”

  6. 在“选择数据连接”屏幕中,连接您的 AdventureWorksLT2008 数据库。 有关详细信息,请参阅 “选择数据连接”对话框

    确保名称为 AdventureWorksLT2008Entities 并且选中了“将 App.Config 中的实体连接设置另存为”复选框,然后单击“下一步”

  7. 在“选择数据库对象”屏幕中,展开“表”节点,然后选择“Product”和 ProductCategory”表

    可以为所有表生成实体类;但是,在此示例中,仅从这两个表中检索数据。

    从表中选择 Product 和 ProductCategory

  8. 单击“完成”。

    Product 和 ProductCategory 实体显示在实体设计器中。

    Product 和 ProductCategory 实体模型

检索和呈现数据

  1. 打开 MainWindow.xaml 文件。

  2. Width 上的 Window 属性设置为 450。

  3. 在 XAML 编辑器中,在 DataGrid<Grid> 标记之间添加以下 </Grid> 标记,以添加名为 DataGriddataGrid1

    <DataGrid Name="dataGrid1" />
    

    具有 DataGridDataGrid_SQL_EF_Step6窗口

  4. 选择 Window

  5. 使用“属性”窗口或 XAML 编辑器,为 WindowWindow_Loaded 事件创建一个名为 Loaded 的事件处理程序。 有关详细信息,请参阅 如何:创建简单事件处理程序

    下面显示了 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>
    
  6. 打开 Window 的代码隐藏文件(MainWindow.xaml.vb 或 MainWindow.xaml.cs)。

  7. 添加以下代码以仅从联接表中检索特定值,并将 ItemsSourceDataGrid 属性设置为查询结果。

    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
    
  8. 运行示例。

    应该会看到一个显示数据的 DataGrid

    使用来自 SQL 数据库的数据的 带有来自 SQL 数据库的数据的 DataGrid

另请参阅