Search code examples
c#linqlinq-to-sqldatacontext

Linq to SQL Server Name Changes


We have a C# desktop app that used datacontext for linq to sql. It's running well for quite and while and then the IT guys move the database to a different server. Ouch, the app had to be recompiled with the new server name.

Is there a way to set the server name at run time and still use linq to sql? I have not been able to find a way to modify the datacontext at run time. Has anyone found a way to have variable database server names and linq to sql?

I know I can switch to staring sql commands and handle it there but I'd like to stay with linq.

Any suggestions?


Solution

  • Bernie,

    I'm not sure if this is what you're asking, but you can set the DataContext dynamically by adding the connection string as a parameter when you new-up the dbx. For example:

    var dbx = new DataClasses1DataContext(@"Data Source=YourServer;Initial Catalog=YourDb;Integrated Security=True");
    

    I do this when my software integrates with a variety of backends and needs to figure out which one at run time. Again, I'm not sure if that's what you're after, but if it is, you may find that helpful.