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.

Prerequisites

To complete this quickstart, you need:

  • An Azure SQL database. You can use one of these techniques to create a database:

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, database name, and login information for the upcoming procedures.

  1. Sign in to the Azure portal.

  2. Select SQL databases from the left menu, then select your database on the SQL databases page.

  3. On the Overview page for your database, review the fully qualified server name next to Server name. To copy the server name, hover over it and select the Copy icon.

server-name

If you forget your login information, select the server name to open the SQL server page. Here you can view the Server admin name, and select Reset password if necessary.

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. Substitute your values for <server>, <username>, <password>, and <database>.

    Important

    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)
            {
                try 
                { 
                    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:");
                        Console.WriteLine("=========================================\n");
    
                        connection.Open();       
                        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(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 top 20 Category/Product rows from your database are returned, and then close the app window.

Next steps