Quickstart: Use .NET and C# in Visual Studio to connect to and query a database
Applies to: Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics
This quickstart shows how to use the .NET and C# code in Visual Studio to query a database in Azure SQL or Synapse SQL with Transact-SQL statements.
Prerequisites
To complete this quickstart, you need:
An Azure account with an active subscription. Create an account for free.
Visual Studio 2022 Community, Professional, or Enterprise edition.
A database where you can run a 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
Create code to query the database in Azure SQL Database
In Visual Studio, create a new project.
In the New Project dialog, select the C# Console App.
Enter sqltest for the project name, and then select Next.
Select a (Long-term support) Framework option, such as .NET 6.0, and then select Create. The new project is created.
Select Project > Manage NuGet Packages.
In NuGet Package Manager, select the Browse tab, then search for and select Microsoft.Data.SqlClient.
On the Microsoft.Data.SqlClient page, select Install.
- If prompted, select OK to continue with the installation.
- If a License Acceptance window appears, select I Accept.
When the install completes, you can close NuGet Package Manager.
In the code editor, replace the Program.cs contents with the following code. Replace your values for
<your_server>
,<your_username>
,<password>
, and<your_database>
.using System; using Microsoft.Data.SqlClient; using System.Text; namespace sqltest { class Program { static void Main(string[] args) { try { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "<your_server>.database.windows.net"; builder.UserID = "<your_username>"; builder.Password = "<password>"; builder.InitialCatalog = "<your_database>"; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { Console.WriteLine("\nQuery data example:"); Console.WriteLine("=========================================\n"); String sql = "SELECT name, collation_name FROM sys.databases"; using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1)); } } } } } catch (SqlException e) { Console.WriteLine(e.ToString()); } Console.ReadLine(); } } }
Run the code
- To run the app, select Debug > Start Debugging, or select Start on the toolbar, or press F5.
- Verify that the database names and collations are returned, and then close the app window.
Next steps
- Learn how to connect and query a database in Azure SQL Database by using .NET from the command line on Windows/Linux/macOS.
- Learn about Getting started with .NET on Windows/Linux/macOS using VS Code.
- Learn more about developing with .NET and SQL.
- Learn how to Design your first database in Azure SQL Database by using SSMS.
- For more information about .NET, see .NET documentation.
- Retry logic example: Connect resiliently to Azure SQL with ADO.NET.