This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Applies to:Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
In this quickstart, you'll use.NET and C# code to connect to a database. You'll then run a Transact-SQL statement to query data. This quickstart is applicable to Windows, Linux, and macOS and leverages the unified .NET platform.
Tip
This free Learn module shows you how toDevelop and configure an ASP.NET application that queries a database in Azure SQL Database
To complete this quickstart, you need:
An Azure account with an active subscription.Create an account for free.
.NET SDK for your operating system installed.
A database where you can run your query.
You can use one of these quickstarts to create and then configure a database:
Action | SQL Database | SQL Managed Instance | SQL Server on Azure VM | Azure Synapse Analytics |
---|---|---|---|---|
Create | Portal | Portal | Portal | Portal |
CLI | CLI | |||
PowerShell | PowerShell | PowerShell | PowerShell | |
Deployment template | Deployment template | Deployment template | Deployment template | |
Configure | Server-level IP firewall rule | Connectivity from a VM | Connectivity settings | |
Connectivity from on-premises | Connect to a SQL Server instance | |||
Get connection information | Azure SQL | Azure SQL | SQL VM | Synapse SQL |
Open a command prompt and create a folder namedsqltest. Navigate to this folder and run this command.
dotnet new console
This command creates new app project files, including an initial C# code file (Program.cs), an XML configuration file (sqltest.csproj), and needed binaries.
At the command prompt used above, run this command.
dotnet add package Microsoft.Data.SqlClient
This command adds theMicrosoft.Data.SqlClient
package to the project.
In a text editor such asVisual Studio Code, openProgram.cs.
Replace the contents with the following code and add the appropriate values for your server, database, username, and password.
Note
To use an ADO.NET connection string, replace the 4 lines in the codesetting the server, database, username, and password with the line below. Inthe string, set your username and password.
builder.ConnectionString="<connection-string>";
using Microsoft.Data.SqlClient;using System;using System.Threading.Tasks;namespace sqltest{ class Program { static async Task Main(string[] args) { var builder = new SqlConnectionStringBuilder { DataSource = "<your_server.database.windows.net>", UserID = "<your_username>", Password = "<password>", InitialCatalog = "<your_database>" }; var connectionString = builder.ConnectionString; try { await using var connection = new SqlConnection(connectionString); Console.WriteLine("\nQuery data example:"); Console.WriteLine("=========================================\n"); await connection.OpenAsync(); var sql = "SELECT name, collation_name FROM sys.databases"; await using var command = new SqlCommand(sql, connection); await using var reader = await command.ExecuteReaderAsync(); while (await reader.ReadAsync()) { Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1)); } } catch (SqlException e) when (e.Number == /* specific error number */) { Console.WriteLine($"SQL Error: {e.Message}"); } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine("\nDone. Press enter."); Console.ReadLine(); } }}
Remember to replace<your_server.database.windows.net>
,<your_username>
,<password>
, and<your_database>
with your actual SQL Server details. Also, replace/* specific error number */
with the actual SQL error number you want to handle.
At the prompt, run the following commands.
dotnet restoredotnet run
Verify that the rows are returned, your output might include other values.
Query data example:=========================================master SQL_Latin1_General_CP1_CI_AStempdb SQL_Latin1_General_CP1_CI_ASWideWorldImporters Latin1_General_100_CI_ASDone. Press enter.
ChooseEnter to close the application window.
Was this page helpful?
Was this page helpful?