none
programmatically changing Sql Reference for LINQ to Sql RRS feed

  • Question

  • Is there a way to programmatically change the SQL DB reference in the dbml file? I know you can go into the designer and just refresh the db in the server explorer and it regenerates, but is there a different way to do it?  Maybe in code?

    Thanks
    Wednesday, March 26, 2008 3:38 PM

Answers

  • Hi,

     

    the default constructor of the datacontext uses the settings from the application. Since they are stored in xml file <program name>.exe.config you can modify the connection string there (under the assumption that you use the default constructor).

    I'm not sure if Linq uses the dbml file at runtime since this file isn't included in bin directory and do not know if you can change mappings during runtime.

    But it's a good question.

    This is of interest in other situations too: What happens if you change table names in a multi tier application and you don't want to redistribute the binaries to all clients?

     

    regards

    Philipp

    Wednesday, March 26, 2008 5:55 PM

All replies

  • Hi,

     

    when you build the instance of your DataContext you can pass a parameter to the constructor. This can be a SqlConnection object or the connection string.

     

    regards

    Philipp

    Wednesday, March 26, 2008 3:54 PM
  • I saw that. I guess I was more thinking about a scenerio where an application is released as open source along with scripts to run to build the proper table(s) and proc(s).  I was trying to think of another way that a someone could D'L the app, run the scripts and not have to open the app to reference their connection string or db.  Is that possible?  I feel like the answer is something easy, but I just am looking past it for some reason.

    Thanks.
    Wednesday, March 26, 2008 4:27 PM
  • Hi,

     

    the default constructor of the datacontext uses the settings from the application. Since they are stored in xml file <program name>.exe.config you can modify the connection string there (under the assumption that you use the default constructor).

    I'm not sure if Linq uses the dbml file at runtime since this file isn't included in bin directory and do not know if you can change mappings during runtime.

    But it's a good question.

    This is of interest in other situations too: What happens if you change table names in a multi tier application and you don't want to redistribute the binaries to all clients?

     

    regards

    Philipp

    Wednesday, March 26, 2008 5:55 PM
  • If you have used Server Explorer as the source for your LINQ to SQL classes it should have added the connection string to the app.config file where it can be changed by users at run-time providing the database matches.

     

    [)amien

     

    Thursday, May 29, 2008 11:27 PM
    Moderator