Quickstart: Use .NET and C# in Visual Studio to connect to and query a database in Azure SQL Database or Azure SQL Managed Instance

APPLIES TO: yesAzure SQL Database yesAzure SQL Managed Instance

This quickstart shows how to use the .NET Framework and C# code in Visual Studio to query a database in Azure SQL Database with Transact-SQL statements.


To complete this quickstart, you need:

Get server connection information

Get the connection information you need to connect to the database. You'll need the fully qualified server name or host name, database name, and login information for the upcoming procedures.

  1. Sign in to the Azure portal.

  2. Navigate to the SQL databases or SQL Managed Instances page.

  3. On the Overview page, review the fully qualified server name next to Server name for a database in Azure SQL Database or the fully qualified server name (or IP address) next to Host for an Azure SQL Managed Instance or SQL Server on Azure VM. To copy the server name or host name, hover over it and select the Copy icon.


For connection information for SQL Server on Azure VM, see Connect to a SQL Server instance.

Create code to query the database in Azure SQL Database

  1. In Visual Studio, select File > New > Project.

  2. In the New Project dialog, select Visual C#, and then select Console App (.NET Framework).

  3. Enter sqltest for the project name, and then select OK. The new project is created.

  4. Select Project > Manage NuGet Packages.

  5. In NuGet Package Manager, select the Browse tab, then search for and select System.Data.SqlClient.

  6. On the System.Data.SqlClient page, select Install.

    • If prompted, select OK to continue with the installation.
    • If a License Acceptance window appears, select I Accept.
  7. When the install completes, you can close NuGet Package Manager.

  8. In the code editor, replace the Program.cs contents with the following code. Replace your values for <server>, <username>, <password>, and <database>.


    The code in this example uses the sample AdventureWorksLT data, which you can choose as source when creating your database. If your database has different data, use tables from your own database in the SELECT query.

    using System;
    using System.Data.SqlClient;
    using System.Text;
    namespace sqltest
        class Program
            static void Main(string[] args)
                    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                    builder.DataSource = "<server>.database.windows.net"; 
                    builder.UserID = "<username>";            
                    builder.Password = "<password>";     
                    builder.InitialCatalog = "<database>";
                    using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                        Console.WriteLine("\nQuery data example:");
                        StringBuilder sb = new StringBuilder();
                        sb.Append("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName ");
                        sb.Append("FROM [SalesLT].[ProductCategory] pc ");
                        sb.Append("JOIN [SalesLT].[Product] p ");
                        sb.Append("ON pc.productcategoryid = p.productcategoryid;");
                        String sql = sb.ToString();
                        using (SqlCommand command = new SqlCommand(sql, connection))
                            using (SqlDataReader reader = command.ExecuteReader())
                                while (reader.Read())
                                    Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
                catch (SqlException e)

Run the code

  1. To run the app, select Debug > Start Debugging, or select Start on the toolbar, or press F5.
  2. Verify that the top 20 Category/Product rows from your database are returned, and then close the app window.

Next steps