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 Framework 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:

Create code to query the database in Azure SQL Database

  1. In Visual Studio, create a new project.

  2. In the New Project dialog, select the Visual C#, 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 Microsoft.Data.SqlClient.

  6. 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.
  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 <your_server>, <your_username>, <your_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 = "<your_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

  1. To run the app, select Debug > Start Debugging, or select Start on the toolbar, or press F5.
  2. Verify that the database names and collations are returned, and then close the app window.

Next steps