Connection string in LINQ

It is very common that you come across scenario when at run time you need to change the database server and so the connection string is used in your LINQ. This may come as requirement, if you are moving your application from dev server to staging and from staging to production.

There are two ways you can change connection string on fly

  1. Edit config file
  2. Build connection string to pass as constructor of DataContext.

Edit config file

You can do it in two ways. Open the configuration file and edit connection string here.

clip_image002

In other way, you can change connection string is open DBML file and right click then select Properties

clip_image002[5]

In properties tab create on Connection and click on Connection String

clip_image004

In connection properties dialog box you can change the connection so the connection string.

clip_image006

Editing Connection String in code

Assuming you is creating a function to return connection string. Crete function that it takes server name as input parameter. Your function should be like below.

clip_image008

You can pass server name to this function to create connection string and use output of this function in constructor of DataContext

clip_image010

So here you can pass any server name to create DataContext as of server.

For reference code is as below ,

 static string GetConnectionString(string serverName)
        {

            System.Data.SqlClient.SqlConnectionStringBuilder builder =
                           new System.Data.SqlClient.SqlConnectionStringBuilder();
            builder["Data Source"] = serverName;
            builder["integrated Security"] = true;
            builder["Initial Catalog"] = "Sample2";
            Console.WriteLine(builder.ConnectionString);
            Console.ReadKey(true);
            return builder.ConnectionString;

        }

I hope this post was useful. Thanks for reading.  Smile

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Step by Step walkthrough on CRUD Operation on Azure table part # 2: Setting UP storage account for Azure and modifying connection string

Objective

This article is going to explain , how we can use AZURE table. This article will explain how to create a stroage account and then modify the connection string in exsiting azure application using local development storage.

Note : This article is part 2 of series of article , so I recoomend please go through this article before redaing this one.

You can see video of this article here

Step 1

Login to your Azure portal with your live credential. After your successful login , you can see the below screen . Just click on New Service.

clip_image002

Step 2

Then you need to select Storage account from given options. As you going to create account for windows azure storage .

clip_image004

Step 3

In this step , you need to provide service label and service description .

clip_image006

Step 4

You have to provide a unique name here. I am providing debugmode as unique name.

clip_image008

Step 5

In this step you need to set the affinity group. If you don’t want to put your storage account in a particular affinity group, you can select the region of your own.

clip_image010

And now click on Create

Step 6

Once you click create, you can see three end points has been created for you in azure storage.

clip_image012

Note: Primary access key and secondary access key has been blacked out

Accessing Windows Azure table

If you are referring the article here , then navigate to step 5. Step 5 explains how to set the connection string. You will have to modify the connection string to use Azure table.

Step 1

Click on Webrole 1 in solution explorer.

clip_image013

Step 2

Click on the setting in the tab

clip_image014

Step 3

In the top select add setting

clip_image015

Step 4

When you add a setting a setting will get added. Now from drop down , select Connection string as type.

clip_image016

Step 5

When you click to set the value, you will get the below child window. Select Enter storage credentials

clip_image017

Now you need to provide

1. Account name

2. Account Key

Now to provide these two information , copy and paste below two things from your azure portal. In my case account name is debugmode and account key (The blacked out) .

clip_image018

Once you provide these two details, leave the default setting and press ok.

clip_image019

You can see in setting tab a value has been added.

Next and last step , what you need to do is open WebRole.CS file and put the setting name from previous step at these two below places .

clip_image021

After performing above steps, now your application has been modified to use Azure table.

I hope this article was useful. Thanks for reading Happy Coding.