This Azure tutorial will discuss how to connect to Azure SQL database using C#
Table of Contents
How to connect to Azure SQL database from Visual Studio 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.
To connect to Azure SQL database from Visual Studio c#, follow the below steps.
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.

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.

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

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.

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

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

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.

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();
}
}
}


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.

You can check out How to get Azure SQL database connection string
You may like the following Azure tutorials:
- Python Connect to Azure SQL Database
- Asp.Net Connect To Azure SQL Database
- Powershell Connect To Azure SQL Database
- Connect To Azure SQL From SSMS
Conclusion
We discussed how to connect to Azure SQL Database from C#.
I am Bijay, a Microsoft MVP (10 times) having more than 17 years of experience in the software industry. During my IT career, I got a chance to share my expertise in SharePoint and Microsoft Azure, like Azure VM, Azure Active Directory, Azure PowerShell, etc. I hope you will learn from these Azure tutorials. Read more
