---
title: "How to Set database connection string at run time?"  
description: "How to Set database connection string at run time?"  
author: "Allen Scott"  
published: 2014-11-21  
updated: 2014-11-21  
canonical: https://www.mindstick.com/forum/12669/how-to-set-database-connection-string-at-run-time  
category: "asp.net"  
tags: ["c#", "ado.net", "sql server"]  
reading_time: 1 minute  

---

# How to Set database connection string at run time?

I am working on a MVC 4 [web site](https://www.mindstick.com/articles/12285/the-finest-web-site-design-trends-you-may-expect-in-2017). It should allow the user to select which [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) to be [connected](https://www.mindstick.com/articles/12941/link-building-and-search-engine-rankings-how-are-they-connected) with depending on user [selection](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) from the View. All the [databases](https://www.mindstick.com/startup/34/airbyte-the-open-source-platform-simplifying-data-integration-for-warehouses-lakes-and-databases) have the same table [structure](https://www.mindstick.com/articles/23258/choose-your-business-structure-wisely) and [schema](https://www.mindstick.com/articles/1844/how-schema-markup-useful-in-serp) etc.

I have a database ConnectionString defined in the [Web.config](https://www.mindstick.com/forum/183/web-config-file) file that allows for [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) to the first database.

```
<connectionStrings>    <add name="DBConnectionString" connectionString="Data Source=DATABSE_SERVER;Initial Catalog=DATABASE_NAME;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD" providerName="System.Data.SqlClient" /></connectionStrings>
```

I also used Linq DataContext to initialize connection to the database. Table mappings were all automatically generated by [Linq to SQL](https://www.mindstick.com/articles/338528/how-to-use-linq-to-sql-select-query-using-c-sharp) in MVC 4.

public NEMP_DataDataContext() :

```
   base(global::System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, mappingSource){    OnCreated();}
```

What is the best way to achieve this?

## Replies

### Reply by marcel ethan

In LINQ to SQL, you just create the Context with the connection string:

```
DataContext myContext = new DataContext(customConnectionString);
```

You don't need to worry about closing them or anything. SQL Server is handling all that for you. So you simply adjust the connection string slightly (Database Name) and create a new context based on whatever variable in every page you need to use the Context.


---

Original Source: https://www.mindstick.com/forum/12669/how-to-set-database-connection-string-at-run-time

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
