在 Windows 应用中使用 SQL Server 数据库

通过使用 System.Data.SqlClient 命名空间中的类,你的应用可以直接连接到 SQL Server 数据库然后存储和检索数据。

使用入门

在本指南中,我们将介绍一种在 Windows 应用 SDK 应用中执行此操作的方法。 如果你将 Northwind 示例数据库安装到 SQL Server 实例上,然后使用这些代码段,你最终将获得显示了来自 Northwind 示例数据库的产品的基本 UI。

Northwind 产品

本指南中显示的代码段基于此 UWP 示例应用

首先,设置你的解决方案

若要将应用直接连接到 SQL Server 数据库,应用可以面向 Windows 应用 SDK 支持的任何最低版本的 Windows。 可以在项目的属性页中找到该信息。

  1. 在清单设计器中打开 Windows 应用 SDK 项目的 Package.appxmanifest 文件。
  2. “功能”选项卡中,如果要使用 Windows 身份验证对 SQL Server 进行身份验证,则选中“企业身份验证”复选框。

企业身份验证功能

重要

无论你是否使用 Windows 身份验证,都需要选择“Internet (客户端和服务器)”、“Internet (客户端)”和“私有网络(客户端和服务器)”

在 SQL Server 数据库中添加和检索数据

在本部分中,我们将执行以下操作:

1️⃣ 添加连接字符串。

2️⃣ 创建用于保存产品数据的类。

3️⃣ 从 SQL Server 数据库检索产品。

4️⃣ 添加基本用户界面。

5️⃣ 使用产品填充 UI。

注意

本节介绍了一种组织你的数据访问代码的方法。 这不仅仅是为了提供如何使用 System.Data.SqlClient 在 SQL Server 数据库中存储和检索数据的示例。 你可以采用对你的应用程序设计最有意义的任何方式组织你的代码。

添加连接字符串

App.xaml.cs 文件中,向 App 类添加一个属性,该属性为你的解决方案中的其他类提供了对连接字符串的访问权限。

我们的连接字符串指向 SQL Server Express 实例中的 Northwind 数据库。

sealed partial class App : Application
{
    // Connection string for using Windows Authentication.
    private string connectionString =
        @"Data Source=YourServerName\SQLEXPRESS;Initial Catalog=NORTHWIND;Integrated Security=SSPI";

    public string ConnectionString { get => connectionString; set => connectionString = value; }

    ...
}

重要

在生产应用程序中,连接信息应安全地存储在应用配置中(请参阅使用 Visual Studio Connected Services 添加 Azure 应用配置)。 连接字符串和其他机密永远不应该被硬编码。

创建用于保存产品数据的类

我们将创建一个实现 INotifyPropertyChanged 事件的类,以便将 XAML UI 中的属性绑定到此类中的属性。

public class Product : INotifyPropertyChanged
{
    public int ProductID { get; set; }
    public string ProductCode { get { return ProductID.ToString(); } }
    public string ProductName { get; set; }
    public string QuantityPerUnit { get; set; }
    public decimal UnitPrice { get; set; }
    public string UnitPriceString { get { return UnitPrice.ToString("######.00"); } }
    public int UnitsInStock { get; set; }
    public string UnitsInStockString { get { return UnitsInStock.ToString("#####0"); } }
    public int CategoryId { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

从 SQL Server 数据库检索产品

在 Windows 应用 SDK 项目的 MainWindow.xaml.cs 文件中,创建一个从 Northwind 示例数据库获取产品的方法,然后将这些产品作为 Product 实例的 ObservableCollection 集合返回。

public ObservableCollection<Product> GetProducts(string connectionString)
{
    const string GetProductsQuery = "select ProductID, ProductName, QuantityPerUnit," +
       " UnitPrice, UnitsInStock, Products.CategoryID " +
       " from Products inner join Categories on Products.CategoryID = Categories.CategoryID " +
       " where Discontinued = 0";

    var products = new ObservableCollection<Product>();
    try
    {
        using (var conn = new SqlConnection(connectionString))
        {
            conn.Open();
            if (conn.State == System.Data.ConnectionState.Open)
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = GetProductsQuery;
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var product = new Product();
                            product.ProductID = reader.GetInt32(0);
                            product.ProductName = reader.GetString(1);
                            product.QuantityPerUnit = reader.GetString(2);
                            product.UnitPrice = reader.GetDecimal(3);
                            product.UnitsInStock = reader.GetInt16(4);
                            product.CategoryId = reader.GetInt32(5);
                            products.Add(product);
                        }
                    }
                }
            }
        }
        return products;
    }
    catch (Exception eSql)
    {
        Debug.WriteLine($"Exception: {eSql.Message}");
    }
    return null;
}

添加基本用户界面

将以下 XAML 添加到 Windows 应用 SDK 项目的 MainWindow.xaml 文件。

此 XAML 将创建一个 ListView(用来显示你在上一个代码段中返回的每个产品),并会将 ListView 中的每个行的属性绑定到我们在 Product 类中定义的属性。

<Grid Background="{ThemeResource SystemControlAcrylicWindowBrush}">
    <RelativePanel>
        <ListView Name="InventoryList"
                  SelectionMode="Single"
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  ScrollViewer.IsVerticalRailEnabled="True"
                  ScrollViewer.VerticalScrollMode="Enabled"
                  ScrollViewer.HorizontalScrollMode="Enabled"
                  ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  ScrollViewer.IsHorizontalRailEnabled="True"
                  Margin="20">
            <ListView.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal"  >
                        <TextBlock Text="ID" Margin="8,0" Width="50" Foreground="DarkRed" />
                        <TextBlock Text="Product description" Width="300" Foreground="DarkRed" />
                        <TextBlock Text="Packaging" Width="200" Foreground="DarkRed" />
                        <TextBlock Text="Price" Width="80" Foreground="DarkRed" />
                        <TextBlock Text="In stock" Width="80" Foreground="DarkRed" />
                    </StackPanel>
                </DataTemplate>
            </ListView.HeaderTemplate>
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Product">
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Name="ItemId"
                                    Text="{x:Bind ProductCode}"
                                    Width="50" />
                        <TextBlock Name="ItemName"
                                    Text="{x:Bind ProductName}"
                                    Width="300" />
                        <TextBlock Text="{x:Bind QuantityPerUnit}"
                                   Width="200" />
                        <TextBlock Text="{x:Bind UnitPriceString}"
                                   Width="80" />
                        <TextBlock Text="{x:Bind UnitsInStockString}"
                                   Width="80" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </RelativePanel>
</Grid>

在 ListView 中显示产品

打开 MainWindow.xaml.cs 文件,并将代码添加到 MainWindow 类的构造函数,该类可将 ListViewItemSource 属性设置为 Product 实例的 ObservableCollection

public MainWindow()
{
    this.InitializeComponent();
    InventoryList.ItemsSource = GetProducts((App.Current as App).ConnectionString);
}

启动项目并看到来自 Northwind 示例数据库的产品出现在 UI 中。

Northwind 产品

探索 System.Data.SqlClient 命名空间以了解 SQL Server 数据库中的数据的其他作用。

连接数据库时遇到问题?

在大多数情况下,需要更改 SQL Server 配置的某些方面。 如果能够从其他类型的桌面应用程序(例如 Windows 窗体或 WPF 应用程序)连接到数据库,请确保已为 SQL Server 启用 TCP/IP。 可以在“计算机管理”控制台中执行该操作。 (有关详细信息,请参阅 Windows 工具/管理工具。)

筿福恨瞶

然后,确保 SQL Server Browser 服务正在运行。

SQL Server Browser 服务

下一步