共用方式為


逐步解說:在 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 Designer],然後點擊 [下一步]

  6. 在 [選擇您的資料連線] 畫面中,建立與 AdventureWorksLT2008 資料庫的連線。 如需詳細資訊,請參閱選擇您的資料連線對話方塊

    請確定名稱為 AdventureWorksLT2008Entities,且已選取 [將 App.Config 中的實體連線設定儲存為] 核取方塊,然後按下 [下一步]

  7. 在 [選擇您的資料庫物件] 畫面中,展開 [資料表] 節點,然後選取 [產品] 和 [ProductCategory] 資料表。

    您可以為所有資料表產生實體類別;不過,在此範例中,您只會從這兩個資料表擷取資料。

    從資料表選取 [產品] 和 [ProductCategory]

  8. 按一下完成

    產品和 ProductCategory 實體會顯示在 Entity Designer 中。

    產品和 ProductCategory 實體模型

擷取並呈現資料

  1. 開啟 MainWindow.xaml 檔案。

  2. Width 上的 Window 屬性設定為 450。

  3. 在 XAML 編輯器中,在 DataGrid<Grid> 標籤之間新增下列 </Grid> 標籤,以新增名為 DataGriddataGrid1

    <DataGrid Name="dataGrid1" />
    

    具有 DataGrid 的視窗

  4. 選取 Window

  5. 使用 [屬性] 視窗或 XAML 編輯器,為 Window 事件名為 Window_LoadedLoaded 建立事件處理常式。 如需詳細資訊,請參閱 操作說明:建立簡單的事件處理常式

    下列顯示 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.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.Linq
    
    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 query = _
                From product In dataEntities.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 資料庫之資料的 DataGrid

另請參閱