default connection string when using linq to sql


Well-known member
Nov 13, 2012
Programming Experience
In an existing C# 2010 application, I want to add linq to sql statements to connect to a sql server 2008 database. When I setup this linq to sql database, I want to be able to setup the connections to the
database using the app.config file.
I have seen that when you setup the database connection this way, the default database is used. Basically this is ok when you database on the same database. However, when you move from a unit testing database to a user acceptance and/or a production database there is a problem since the database connection is hard coded in the aqpplication.
Thus can you tell me and/or show me with code how to prevent this problem from occuring?
There is no problem and that's the whole point of the connection string being stored in the config file. When you create LINQ to SQL classes the connection string is generated and stored in the config file. When you move the application to another location and you need to connect to a different database you simply edit that connection string in the config file by hand. The connection string is NOT hard-coded in the application. The config file is NOT part of the compiled application. It is an external data file that you can edit by hand nay time you like after deployment. That's the whole point.
The connection string is not hard-coded in the application, the value is saved in the application settings file. How can you use the config file to override the value in the application settings value? Stated differently, I just want to use the app.config file value. I do not want to use the value in the application settings file.
Top Bottom