Azure SQL Database APIs for .NET
Overview
Azure SQL Database is a database service using the Microsoft SQL Server engine that supports relational, JSON, spatial, and XML data.
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
Use the .NET SQL client library to connect and authenticate with your database and execute ad-hoc T-SQL statements and stored procedures.
Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.
Visual Studio Package Manager
Install-Package System.Data.SqlClient
.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
Use the Azure SQL Database management library to create, manage, and scale Azure SQL Database server instances.
Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.
Visual Studio Package Manager
Install-Package Microsoft.Azure.Management.Sql.Fluent
.NET Core command line
dotnet add package Microsoft.Azure.Management.Sql.Fluent
Code Example
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
View the complete list of Azure SQL Database samples.