I created the code which fine work with my production database. But I want easy way for changing database name. It is way will be without further action, and code duplication. How I can do it?
I having databeses "MyDB_1", "MyDB_2" - it is full of copies of databases for testing and like that.
The way to change the connection via ODBC is also nice. But how i can do it?
DataTrackerClassesDataContext ctx = new DataTrackerClassesDataContext();
// !!! I cant change the property because it's readonly:
//ctx.Connection.Database = "MyDB_1";
//ctx.Connection.Database = "MyDB_2";
Console.WriteLine(ctx.Connection.Database);
var custQuery =
from m in ctx.models
where m.status == 1
select m.id;
foreach (int id in custQuery)
{
Console.WriteLine("{0} ", id);
}
If you want to switch the database for a datacontext, then in the constructor for a datacontext you would input the new connection string.
You would do it like this to maintain the format:
string connectionString =
string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True",
serverName,
dataBaseName);
DataTrackerClassesDataContext ctx = new DataTrackerClassesDataContext(connectionString);
And you would just replace the connection string based on which database you wanted to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With