Archive

Posts Tagged ‘connection strings’

Windows Azure: SQL Azure Database connection string

June 3, 2011 Leave a comment

In this article I will show you how to get connection string for your application to connect to your SQL Azure database.

If you have had experience developing ASP.NET application, the connection string below might be familiar to you.

Data Source=<DATABASE SERVER>;Initial Catalog=<DATABASE>;User ID=<USERNAME>;Password=<PASSWORD>

However in Windows Azure Platform, the connection string is a little bit different from the above. Rather of wasting time figuring out what the connection string to your database is, you can actually get the connection string to your specific Windows Azure SQL Database right from the Windows Azure Platform Management Portal.

Assuming that you have already create your SQL Azure Database, follow the steps below to get your connection string from the Windows Azure Management Portal. If you haven’t do so, see my previous post on How to create a SQL Azure Database.

  1. Login to Windows Azure Management Portal using your Live ID at http://windows.azure.com/
  2. Once you’re logged in, click on the Database button at the lower left of the page.
  3. Click on the database you wish to view it’s connection string. Then on the Properties panel at the right side of the page, click on the button next to View.

    azure_mgmtPortal_content_database_connectionString

    Now, you will be able to see 3 different connection string for your database. Copy & Paste the relevant connection string.

    IMPORTANT: Review that you want to use the UserName to connect to the database. Also, you will need to change the Password for the selected user.

Advertisements

Getting ConnectionStrings value from code behind

December 5, 2010 Leave a comment

When you do wants to get some data from a database to show it on your ASP.NET web application, you will need to use a connection string to connect to the database. This string is very important because, at most conditions, you will be using this string again and again across the entire application. Therefore, you can utilize the ConnectionStrings tag in the web.config file to store your predefined connection strings.

Well, some programmers will say “Why do I need to store it in the web.config file? I can just code it in my code behind codes and it will still work the same.”. Yes, it will still work the same. But that is only suitable for a mini web application that does not require much connection to the database. However, if it’s a big web application and the connection string values have to be changed, the programmers will spend a long time just to find and replace the existing connection string to the new string.

Therefore, it’s advised to always store a connection string in the web.config file and get its value from here. Below, I will show you how to get the connection string values from the ConnectionStrings tag in the web.config file.

 

Code in web.config:

<connectionStrings>
        <add name="MyConnectionString" connectionString="Your connection string here" providerName="System.Data.SqlClient" />
</connectionStrings>

Code for C#:

System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

Code for VB.NET:

System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString

From the sample above, I’ve named my connection string to “MyConnectionString”. For the connectionString values which I’ve written as “Your connection string here”, you will have to replace it with a proper connection string. You can find more about connection string from http://connectionstrings.com/. As for the providerName, you have to put the namespace of the data connection class. In this case above, “System.Data.SqlClient” will be connecting to Microsoft SQL Server.