-1

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?

Bernie Hunt
  • 306
  • 4
  • 22
  • 1
    Db server name has nothing to do with Linq. You just neeed to put the connection string to your server to config file. This way when you change db server again, you will need only to change a string in the config file. – Serge Jan 07 '21 at 15:14
  • Sergey, which config file? Or do you mean config file as generic place to store configuration? – Bernie Hunt Jan 08 '21 at 03:33
  • I mean web.config, app.config or appsettings.json. I don't what net framework are you using. – Serge Jan 08 '21 at 08:14
  • So the datacontext can be built against one server and then just change the connection string when instantiating the datacontext to point to another server? – Bernie Hunt Jan 08 '21 at 19:24
  • datacontext is built against database, not the server. Db can be moved to any server – Serge Jan 08 '21 at 19:25

1 Answers1

0

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.

Jim Simson
  • 2,774
  • 3
  • 22
  • 30