In my development environment, my SQL Server is PHILIP\SQLEXPRESS. In testing, it’s ANNIE, and the live environment will have a third name yet to be determined. I would have assumed that updating the following statement in web.config would have been enough:
<add name="MyConnectionString"providerName="System.Data.SqlClient"
connectionString="Data Source=PHILIP\SQLEXPRESS;Initial Catalog=MyDtabase;Integrated Security=True" />
When using SqlConnection, SqlCommand, SqlDataReader and friends, that’s all it took. Using LINQ, it doesn’t seem to work that nicely. I see the servername repeated in my .dbml file as well as in Settings.settings. After changing it in all of those places, I get it to work. However if I’m doing a few deployments per day during testing, I want to avoid this regimen.
My question is: is there a programmatic solution for LINQ to SQL that will allow me to specify the connection string once, preferably in web.config, and get everybody else to refer to it?
— EDIT —
The following connection statement is in my .dbml file:
<Connection Mode="AppSettings" ConnectionString="Data Source=PHILIP\SQLEXPRESS;
InitialCatalog=MyDatabase;Integrated Security=True"
SettingsObjectName="MyDatabase_Data_Access_Layer.Properties.Settings"
SettingsPropertyName="MyConnectionString" Provider="System.Data.SqlClient" />
On your Linq-to-SQL model (the *.dbml file), you should be able to bring up its properties. In the property grid, there is a
Connectionsetting which allows you to specify where and how to store that connection information for Linq-to-SQL. This typically refers to your app.config/web.config. See my sample here:(full size image at http://i43.tinypic.com/xazbrb.png )
There’s nothing stopping you from using the very same connection string as your “normal” app uses – actually, that’s quite a good idea, in fact!
So you should be able to basically change your SQL Server connection string in one place (in your app.config / web.config) and that should be all you need to do! Doesn’t get much easier than that, does it?