Quickstart: Use .NET Core (C#) to query a database

APPLIES TO: Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics

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.

Tip

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

Prerequisites

To complete this quickstart, you need:

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.

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

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.

Note

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.

builder.ConnectionString="<your_ado_net_connection_string>";

using System;
using System.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 = "<your_password>";     
                builder.InitialCatalog = "<your_database>";
         
                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    Console.WriteLine("\nQuery data example:");
                    Console.WriteLine("=========================================\n");
                    
                    connection.Open();       

                    String sql = "SELECT name, collation_name FROM sys.databases";

                    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(e.ToString());
            }
            Console.WriteLine("\nDone. Press enter.");
            Console.ReadLine(); 
        }
    }
}

Run the code

  1. At the prompt, run the following commands.

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

    Query data example:
    =========================================
    
    master	SQL_Latin1_General_CP1_CI_AS
    tempdb	SQL_Latin1_General_CP1_CI_AS
    WideWorldImporters	Latin1_General_100_CI_AS
    
    Done. Press enter.
    
  3. Choose Enter to close the application window.

Next steps