Quickstart: Use .NET and C# in Visual Studio to connect to and query an Azure SQL database

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


To complete this quickstart, you need:

Get SQL server connection information

Get the connection information you need to connect to the Azure SQL 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 single database or the fully qualified server name next to Host for a managed instance. To copy the server name or host name, hover over it and select the Copy icon.

Create code to query the 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