Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘sql azure connect c# example’

Windows Azure – Connect to SQL Azure

Posted by JP on November 13, 2011

We are now are ready with our SQL Azure database server and database. So we can proceed with connecting with the database. We can use our ADO.NET Connection class to connect to the database.

The steps involved here are:

  • Get the Database Server Name
  • Connect using SqlConnection class

The steps are following.

Get the Database Server Name

Sign in to the windows azure portal and click on the Database item from the left pane.

clip_image002

Then expand the subscriptions item and select the database server name. From the right hand side properties window, you can get the Fully qualified DNS Name of the server as shown in the figure below.

clip_image004

Copy your server name. Now open Visual Studio and create a new Azure project and add a web role into it.

Add a SqlDataSource component into it from the toolbox. In the properties window of the control, click on the ConnectionString property. From the drop down list select New Connection. You will be prompted with the following dialog.

clip_image006

Select Microsoft SQL Server from the list above and click continue. Now the Add Connection dialog will appear as shown below.

clip_image008

Enter the following

  • SQL Azure Server Name
  • User Name
  • Password

(Remember to check the Use SQL Server Authentication check box)

Now select the database we created and press Test Connection button. If you get the succeeded message box, you have a valid SQL Azure connection. Good Job!

clip_image010

Now press OK on the Add Connection dialog and from the properties window, you can collect the connection string as shown below.

clip_image012

Connect using SqlConnection class

Now we have the valid connection string, we can try connecting through code.

Open the code view of Default.aspx from the web role project. Add the following code into it. (Resolve the namespaces)

protected void Page_Load(object sender, EventArgs e)

{

string connectionString = “your connection string here”;

SqlConnection connection = new SqlConnection(connectionString);

connection.Open();

if (connection.State == System.Data.ConnectionState.Open)

Label1.Text = “Connection Succeeded!”;

else

Label1.Text = “Connection Failure!”;

}

Open the design view of Default.aspx and add a label control into it. You may remove the data source control from there.

Now execute the application and you can see the result as shown below.

clip_image014

If the message is Connection Succeeded you are having a valid connection established with SQL Azure.

Collecting Connection String from Windows Azure Portal

We can also get the connection string from the azure portal by selecting the database and clicking the View connection string button.

clip_image016

Then the following dialog box shows the connection string. The first one ADO.NET represents our needed connection string.

Summary

In this article we have seen how to get the database server name, get the connection string and connect it using c# code.

Advertisements

Posted in Azure | Tagged: , , , , , | Leave a Comment »