Quickstart: Use .NET Core (C#) to query a database in Azure SQL Database or Azure SQL Managed Instance

APPLIES TO: yesAzure SQL Database yesAzure SQL Managed Instance

In this quickstart, you'll use .NET Core and C# code to connect to a database. You'll then run a Transact-SQL statement to query data.


The following Microsoft Learn module helps you learn for free how to Develop and configure an ASP.NET application that queries a database in Azure SQL Database


To complete this quickstart, you need:


This quickstart uses the mySampleDatabase database. If you want to use a different database, you will need to change the database references and modify the SELECT query in the C# code.

Get server connection information

Get the connection information you need to connect to the database in 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 the 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.

Get ADO.NET connection information (optional - SQL Database only)

  1. Navigate to the mySampleDatabase page and, under Settings, select Connection strings.

  2. Review the complete ADO.NET connection string.

    ADO.NET connection string

  3. Copy the ADO.NET connection string if you intend to use it.

Create a new .NET Core project

  1. Open a command prompt and create a folder named sqltest. 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.

  2. In a text editor, open sqltest.csproj and paste the following XML between the <Project> tags. This XML adds System.Data.SqlClient as a dependency.

        <PackageReference Include="System.Data.SqlClient" Version="4.6.0" />

Insert code to query the database in Azure SQL Database

  1. In a text editor, open Program.cs.

  2. Replace the contents with the following code and add the appropriate values for your server, database, username, and password.


To use an ADO.NET connection string, replace the 4 lines in the code setting the server, database, username, and password with the line below. In the string, set your username and password.


using System;
using System.Data.SqlClient;
using System.Text;

namespace sqltest
    class Program
        static void Main(string[] args)
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

                builder.DataSource = "<your_server.database.windows.net>"; 
                builder.UserID = "<your_username>";            
                builder.Password = "<your_password>";     
                builder.InitialCatalog = "<your_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)
            Console.WriteLine("\nDone. Press enter.");

Run the code

  1. At the prompt, run the following commands.

    dotnet restore
    dotnet run
  2. Verify that the top 20 rows are returned.

    Query data example:
    Road Frames HL Road Frame - Black, 58
    Road Frames HL Road Frame - Red, 58
    Helmets Sport-100 Helmet, Red
    Helmets Sport-100 Helmet, Black
    Socks Mountain Bike Socks, M
    Socks Mountain Bike Socks, L
    Helmets Sport-100 Helmet, Blue
    Caps AWC Logo Cap
    Jerseys Long-Sleeve Logo Jersey, S
    Jerseys Long-Sleeve Logo Jersey, M
    Jerseys Long-Sleeve Logo Jersey, L
    Jerseys Long-Sleeve Logo Jersey, XL
    Road Frames HL Road Frame - Red, 62
    Road Frames HL Road Frame - Red, 44
    Road Frames HL Road Frame - Red, 48
    Road Frames HL Road Frame - Red, 52
    Road Frames HL Road Frame - Red, 56
    Road Frames LL Road Frame - Black, 58
    Road Frames LL Road Frame - Black, 60
    Road Frames LL Road Frame - Black, 62
    Done. Press enter.
  3. Choose Enter to close the application window.

Next steps