LINQ to SQL connectionstring

前端 未结 6 2201
南旧
南旧 2020-12-09 22:29

I have an application that I want to be able to configure the connection string for my LINQ to SQL. I\'ve tried so many different ways but cannot seem to get it working. I w

6条回答
  •  醉酒成梦
    2020-12-09 23:07

    I think that the best way to do it is a combination of Albin's and Rup's answers. Have a value in the config file, and then read it at run time and feed it to the context constructor, something like this:

    WEB.CONFIG:

    
    
    

    CODE:

    //read value from config
    var DBConnString = System.Configuration.ConfigurationManager.AppSettings("ConString");
    
    //open connection
    var dataContext= new MyDataContext(sDBConnString)
    

    this way you can change the connection string even at runtime and it will work and change on the running program.

提交回复
热议问题