您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 .NET 的 Azure SQL 数据库 APIAzure SQL Database APIs for .NET

概述Overview

Azure SQL 数据库是使用 Microsoft SQL Server 引擎的数据库服务,支持关系型数据、表数据、JSON 数据、空间数据和 XML 数据。Azure SQL Database is a database service using the Microsoft SQL Server engine that supports relational, JSON, spatial, and XML data.

若要详细了解如何在 .NET 中使用 SQL 数据库,请参阅使用 .NET 和 Visual Studio 来连接和查询 Azure SQL 数据库To learn more about the using SQL Database with .NET, see Use .NET with Visual Studio to connect and query an Azure SQL database.

客户端库Client library

使用 .NET SQL 客户端库可以连接到数据库并在其中进行身份验证,以及执行即席 T-SQL 语句和存储过程。Use the .NET SQL client library to connect and authenticate with your database and execute ad-hoc T-SQL statements and stored procedures.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package System.Data.SqlClient

.NET Core CLI.NET Core CLI

dotnet add package System.Data.SqlClient

代码示例Code Example

此示例连接到数据库并从表中读取行。This example connects to a database and reads rows from a table.

/* Include this 'using' directive...
using System.Data.SqlClient;
*/

// Always store connection strings securely. 
string connectionString = "Server=tcp:[serverName].database.windows.net;" 
    + "Database=myDataBase;User ID=[loginname]@[serverName];Password=myPassword;"
    + "Trusted_Connection=False;Encrypt=True;";

// Best practice is to scope the SqlConnection to a "using" block
using (SqlConnection conn = new SqlConnection(connectionString))
{
    // Connect to the database
    conn.Open();

    // Read rows
    SqlCommand selectCommand = new SqlCommand("SELECT * FROM MyTable", conn);
    SqlDataReader results = selectCommand.ExecuteReader();
    
    // Enumerate over the rows
    while(results.Read())
    {
        Console.WriteLine("Column 0: {0} Column 1: {1}", results[0], results[1]);
    }
}

管理库Management library

使用 Azure SQL 数据库管理库可创建、管理和缩放 Azure SQL 数据库服务器实例。Use the Azure SQL Database management library to create, manage, and scale Azure SQL Database server instances.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.Management.Sql.Fluent

.NET Core 命令行.NET Core command line

dotnet add package Microsoft.Azure.Management.Sql.Fluent

代码示例Code Example

此示例创建新的 SQL 数据库服务器实例,然后在该实例上创建新数据库。This example creates a new SQL Database server instance and then creates a new database on that instance.

/* Include these 'using' directives...
using Microsoft.Azure.Management.Sql.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
*/

string startAddress = "0.0.0.0";
string endAddress = "255.255.255.255";

// Create the SQL server instance
ISqlServer sqlServer = azure.SqlServers.Define("UniqueServerName")
    .WithRegion(Region.USEast)
    .WithNewResourceGroup("ResourceGroupName")
    .WithAdministratorLogin("UserName")
    .WithAdministratorPassword("Password")
    .WithNewFirewallRule(startAddress, endAddress)
    .Create();

// Create the database
ISqlDatabase sqlDb = sqlServer.Databases.Define("DatabaseName").Create();

示例Samples

查看 Azure SQL 数据库示例的完整列表View the complete list of Azure SQL Database samples.