How To Create A Console Application To Connect To An Azure SQL Database Using Visual Studio

Introduction

In my previous article, you saw how to create an Azure SQL database and how to query the Database in the Azure portal. Azure SQL Database is a relational Database-as-a-Service which provides a high performance, reliable, and secure database used to build data-driven applications and websites. In this blog, you will see how to create a console application to connect to an Azure SQL database using Visual Studio. As a prerequisite, make sure you have installed the latest version of Visual Studio and also, you have created the server-level firewall rule to access the database from your machine.

SQL Database Connection Details

Log in to the Azure Portal.

Click SQL Databases -> vijaiDB (which I have created in my previous article). Click Overview and you could see the server name and connection string details which are required to connect to SQL database.

 

Create a console application using Visual Studio

Open Visual Studio.

Click File->New->Project.

 

Select Console Application template, enter the name and click OK.

 

Right click on the project and then click Manage NuGet Packages.

 

Browse for System.Data.SqlClient and click Install.

 
Replace Program.cs with the below code. Make sure you update the connection string and query based on your database.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data.SqlClient;  
  7. namespace ConnectAzureSQLDB {  
  8.     class Program {  
  9.         static void Main(string[] args) {  
  10.             try {  
  11.                 SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder();  
  12.                 connBuilder.DataSource = "mysqldbserver20171103.database.windows.net";  
  13.                 connBuilder.UserID = "mydbadmin";  
  14.                 connBuilder.Password = "xxxxxxxxxxx";  
  15.                 connBuilder.InitialCatalog = "vijaiDB";  
  16.                 using(SqlConnection connection = new SqlConnection(connBuilder.ConnectionString)) {  
  17.                     connection.Open();  
  18.                     StringBuilder strBuilder = new StringBuilder();  
  19.                     strBuilder.Append("SELECT TOP 10 p.name as ProductName ");  
  20.                     strBuilder.Append("FROM [SalesLT].[Product] as p");  
  21.                     string cmdText = strBuilder.ToString();  
  22.                     using(SqlCommand sqlCmd = new SqlCommand(cmdText, connection)) {  
  23.                         using(SqlDataReader sqlReader = sqlCmd.ExecuteReader()) {  
  24.                             while (sqlReader.Read()) {  
  25.                                 Console.WriteLine("{0}", sqlReader.GetString(0));  
  26.                             }  
  27.                         }  
  28.                     }  
  29.                 }  
  30.             } catch (SqlException e) {  
  31.                 Console.WriteLine(e.InnerException);  
  32.             }  
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  

 

 Hit F5.

 

Result

Thus, in this blog, you saw how to create a console application to connect to an Azure SQL database using Visual Studio.