locked
retrieving connection string RRS feed

  • Question

  • hi

    i'm developing a win form application that needs to setup a connection with the database only once after it is installed. since the connection string varies through different system, what is the best way to store it in a way that the client himself could set it? the variable which holds the connection string shouldn't change each time the application runs. i appreciate any precise and quick answer...

    Tuesday, January 27, 2009 11:11 AM

Answers

  • In the app.config files there's a connectionString element. You should add your connection string here :
    <connectionStrings>        
            <add name="MyConn" connectionString="........" providerName="" />        
    </connectionStrings>      

     You can read it with :

    string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString; 

     You have to reference System.Configuration.dll in your project references

    • Proposed as answer by Kira Qian Thursday, January 29, 2009 2:45 AM
    • Marked as answer by Kira Qian Friday, January 30, 2009 8:19 AM
    Tuesday, January 27, 2009 11:32 AM
  • A Connection string setting can't be changed at runtime. If you create a Form to modify the app.config, i will not affect your application until you restart it.

    Look at Lindia Liu response in this post you may find it usefull

     

    • Proposed as answer by Kira Qian Thursday, January 29, 2009 2:45 AM
    • Marked as answer by Kira Qian Friday, January 30, 2009 8:19 AM
    Tuesday, January 27, 2009 1:27 PM

All replies

  • In the app.config files there's a connectionString element. You should add your connection string here :
    <connectionStrings>        
            <add name="MyConn" connectionString="........" providerName="" />        
    </connectionStrings>      

     You can read it with :

    string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString; 

     You have to reference System.Configuration.dll in your project references

    • Proposed as answer by Kira Qian Thursday, January 29, 2009 2:45 AM
    • Marked as answer by Kira Qian Friday, January 30, 2009 8:19 AM
    Tuesday, January 27, 2009 11:32 AM
  • thanks, but how can i let the client (who has not access to the source code) configure this line and doesn't it change whenever the application runs? i have to provide a visual interface so the client can change it once.
    Tuesday, January 27, 2009 11:38 AM
  • A Connection string setting can't be changed at runtime. If you create a Form to modify the app.config, i will not affect your application until you restart it.

    Look at Lindia Liu response in this post you may find it usefull

     

    • Proposed as answer by Kira Qian Thursday, January 29, 2009 2:45 AM
    • Marked as answer by Kira Qian Friday, January 30, 2009 8:19 AM
    Tuesday, January 27, 2009 1:27 PM
  • 1. Create a .udl (User Data Link) on application launch to C:\Temp

    2. Launch the .udl to create the connection string.

    3. Use a streamreader to read the connection string.

    4. Delete the .udl from C:\Temp

    Adam


    Environment + Scope + Time Constraints + Management + Experience = Approach
    Tuesday, January 27, 2009 8:26 PM