C# Connect To Azure SQL Database

How To Connect To Azure SQL Database

This Azure tutorial will discuss How To Connect To Azure SQL Database using C#

Connect to Azure SQL Database C#

Let’s discuss connecting to the Azure SQL database from Visual Studio c#. As part of this topic, we will discuss using the .NET Framework and C# code in Visual Studio to query the Azure SQL database.

Before we start the actual development, As Prerequisites, we need the following things.

  • The first thing is the Azure Subscription. If you don’t have a free account, create an Azure free account now.
  • The next thing is you should have an Azure SQL Database created. If not, then create one Azure SQL database before we start.
  • Visual Studio 2019, any edition, needs to be installed on your machine. I am using Microsoft Visual Studio Enterprise 2019.

Get Server Name

We need to get the server name from the Azure SQL Database.

1. Login to Azure Portal (https://portal.azure.com/)

2. Navigate to the Azure SQL Database that you have created.

3. Copy the server name from the Database page —-> Overview tab —> Server name. Copy the server name and keep it in a notepad that we have to use in the C# code. You can click on the copy icon to copy the server name.

How To Connect to Azure SQL Database From Visual Studio C#

Code to Query Azure SQL Database

The next step is to code to query the Azure SQL Database using the C# language using Visual Studio 2019. Follow the below steps to write the code to query the Azure SQL Database using the C#.

1. Open Visual Studio 2019 and click on Create New Project.

connect to azure sql database c# entity framework

2. Select the Console App (.NET Framework) on the Create a new project window and click the Next button.

Connect to Azure SQL Database From Visual Studio C#

3. On the Configure your new project window, Provide a name for the Project, Location, and Solution name, and choose the Framework as the latest one. Then click on the Create button on the same window.

connect to azure sql database c# entity framework

Then, it will take a few seconds to create the project. Now, you can see the project has been created successfully.

connect to azure sql database c#

The next step is to right-click on the project name and click on the Manage NuGet Packages link.

asp.net connect to azure sql database

On the  NuGet Package Manager window, select the Browse tab, then search for System.Data.SqlClient and click on that. Now click on the Install button, and it will install the package successfully without any issues.

connect to azure database from visual studio 2019 c#

Now, the next step is to Open the Program.cs file, and you can replace the code

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyDemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SqlConnectionStringBuilder strngbuilder = new SqlConnectionStringBuilder();
                strngbuilder.DataSource = "mytestdb236.database.windows.net";
                strngbuilder.UserID = "rajkishore";
                strngbuilder.Password = "@@@@@@19";
                strngbuilder.InitialCatalog = "MyDemoDB";

                using (SqlConnection mysqlconnection = new SqlConnection(strngbuilder.ConnectionString))
                {
                    Console.WriteLine("\nMy Azure SQL DB:");
               
                    StringBuilder sb = new StringBuilder();
                    sb.Append("SELECT * from EmployeeDetails");
                    String mysql = sb.ToString();

                    using (SqlCommand command = new SqlCommand(mysql, mysqlconnection))
                    {
                        mysqlconnection.Open();
                        using (SqlDataReader datareader = command.ExecuteReader())
                        {
                            while (datareader.Read())
                            {
                                Console.WriteLine("{0} {1}", datareader.GetString(0), datareader.GetString(1));
                            }
                        }
                    }
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.ReadLine();
        }
    }
}
How To Connect To Azure SQL Database Programmatically
connect azure sql database visualstudio c#

Note: Change your DataSource, UserID, Password, and InitialCatalog as per yours in the above code.

Now press F5 to run the application, or you can select Debug > Start Debugging, and you can see we got the desired output as below.

connect azure database c#

You can check out How to get Azure SQL database connection string

You may like the following Azure tutorials:

Conclusion

We discussed how to connect to Azure SQL Database from C#.