SIGN UP MEMBER LOGIN:    
ARTICLE

Connection String in LINQ

Posted by Dhananjay Kumar Articles | LINQ with C# June 25, 2011
When at run time you need to change the database server, you need to use the connection string in your LINQ.
Reader Level:


It is very common to encouner a scenario in which during execution you need to change the database server and so the connection string is used in your LINQ. This might be a requirement, if you are moving your application from a development server to staging and from staging to production.

There are two ways you can change a connection string on the fly:

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

Edit config file

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

Untitled-1.gif

In another way, you can change the connection string is open DBML file and right-click then select Properties:

Untitled-2.gif
In the properties tab create on Connection and click on Connection String:

Untitled-3.gif

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

Untitled-4.gif
Editing Connection String in code

Assuming you are creating a function to return a connection string. Create a function that takes a server name as an input parameter. Your function should be like below.

Untitled-5.gif

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

Untitled-6.gif

So here you can pass any server name to create the DataContext for that server.

For reference the 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.
  

Login to add your contents and source code to this article
share this article :
post comment
 

For some organization may be but yes many of ogranization do it manually... If it is web application , we can get context of hosting server and can automate the connection string according to server

Posted by Dhananjay Kumar Jun 25, 2011

Is the term "promotion" still used to describe moving software from testing to staging and staging to production? Well that does not matter. That is what we called it in the past. The question I really want to ask is if organizations have a standardized way for their applicaiton software to automatically use the database as determined by their status (testing, staging or production)?

Posted by Sam Hobbs Jun 25, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor