Click here to Skip to main content
12,894,700 members (61,080 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 4 Jun 2014

Change LINQ Connection String Dynamically with Programming

, 4 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip shows how to change LINQ-to-SQL connection string at runtime via programming.


It is very common to encounter 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.

Sometimes, needs of the programming projects are on changing data source in LINQ to SQL class. This paper has tried to use Visual Studio features on how to change LINQ to SQL data source at runtime.


You will need the following tools before using the class. These tools are needed for development.

  • Visual Studio .NET 2008 or higher
  • .NET Framework 3.0 or higher

Using the Code

Assume you are creating a function to return a connection string. At first, we create a public static class named Class_MainConStr. We have a method that returns a value of type string. Your function should be like below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqConStr
    public static class Class_MainConStr
        public static String UDF_MainCnStr()
            return Properties.Settings.Default.ProjectConnectionString.ToString().Trim();

Next, we should find connection string method in LINQ to SQL data class .cs file. (See Figure 1.)

Changing linq connection string

Figure 1
[__strong__]public DataClasses1DataContext() : 
base(global::LinqConStr.Properties.Settings.Default.ProjectConnectionString, mappingSource)

At this time, you should replace LinqConStr.Properties.Settings.Default.ProjectConnectionString with LinqConStr.Class_MainConStr.UDF_MainCnStr().

[__strong__]public DataClasses1DataContext() : 
base(global::LinqConStr.Class_MainConStr.UDF_MainCnStr(), mappingSource)

App.config Setting

Application configuration files contain settings specific to an application. This file contains configuration settings that the common language runtime reads (such as assembly binding policy, remote objects, and so on), and settings that the application can read.

Your project connection string should be placed in app.config file. This file is in XML format. With this file, you can change the connection string. You can create this file automatically with Visual Studio. You can store inside it connection string that you may want to change without having to change code. (See Figure 2.)


Figure 2

If you have remote server, you can change connection string as below:

<?xml version="1.0" encoding="utf-8" ?>
        <add name="LinqConStr.Properties.Settings.ProjectConnectionString"

        connectionString="Data Source=YourRemoteServerIP;Initial Catalog=YourDatabaseName;
        User ID=UserID;Password=YourPassword"providerName="System.Data.SqlClient" />


Now you can use this solution in your projects. If you need help, do not hesitate to contact me.

About the Author

I'm a software engineer living in Iran. I like to work on Windows and Web Application and database programming.

Ali Najafzade


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

A. Najafzadeh
Software Developer Bitasoft Co.
Iran (Islamic Republic of) Iran (Islamic Republic of)
Dear All
Thanks for read this words.

Bitasoft Co. was stablished in 2002. Our activity is programming.

We can supply you with best Source Codes and projects.


You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 4 Jun 2014
Article Copyright 2014 by A. Najafzadeh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid