locked
Store ConnectionString in Registry RRS feed

  • Question

  • Hello all,

    I started building a WPF app . The app uses a remote database . The thing is I dont want to use the connectionstring settings in the app.config file as I tried many ways to update it runtime but its not working . So I want to do it in other way . I want to connect to database when when the app first starts up . The new connectionstring which I'll get , want to store in the client's registry . Now everytime the app starts up I'll check the registry if the connectionstring is present (this I'll do in the load event of the main window), if the connection string is found then the app would load data using that connection else search again . Now I dont know to code it properly :( . Can I get some basic help to move on ?

    Thanks

    Dipayan Sukul

    Sunday, July 8, 2012 2:25 AM

Answers

  • The thing is I dont want to use the connectionstring settings in the app.config file as I tried many ways to update it runtime but its not working .

    I think your question is more related to .Net than SQL Server so you might try posting to a .Net forum if this doesn't your questions is not answered here.

    Most .Net applications use the ConnectionStrings section of an app.config.  I don't know what sort of problems you are having but the C# code snippet below adds a connection string to the app.config file:

                var appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(
                    System.Reflection.Assembly.GetExecutingAssembly().Location);
                appConfig.ConnectionStrings.ConnectionStrings.Add(new System.Configuration.ConnectionStringSettings(
                    @"MyDatabase"
                    , @"Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=SSPI"));
                appConfig.Save();

    The connection string can be retrieved with:

    var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabase"];

    A null will be returned if the connection string is not found in the app.config and you can take the appropriate action.

     

    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    • Marked as answer by Maggie Luo Sunday, July 15, 2012 4:47 PM
    Sunday, July 8, 2012 3:05 PM
    Answerer

All replies

  • Would you please check-out below thread .

    http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/a836c2e9-db92-481c-a85c-ac5d3e764eeb


    Regards,
    Ahmed Ibrahim
    SQL Server Setup Team
    My Blog
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
    This can be beneficial to other community members reading the thread.

    Sunday, July 8, 2012 5:37 AM
  • Hi Ahmed ,

    I checked that thread before posting here . I didnt get it so I was looking for something simpler .

    Thanks

    Dipayan Sukul

    Sunday, July 8, 2012 12:10 PM
  • I think you need to create such registry path and from within a application read it when it starts.

    Best Regards,Uri Dimant SQL Server MVP,http://sqlblog.com/blogs/uri_dimant/

    Sunday, July 8, 2012 12:11 PM
    Answerer
  • The thing is I dont want to use the connectionstring settings in the app.config file as I tried many ways to update it runtime but its not working .

    I think your question is more related to .Net than SQL Server so you might try posting to a .Net forum if this doesn't your questions is not answered here.

    Most .Net applications use the ConnectionStrings section of an app.config.  I don't know what sort of problems you are having but the C# code snippet below adds a connection string to the app.config file:

                var appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(
                    System.Reflection.Assembly.GetExecutingAssembly().Location);
                appConfig.ConnectionStrings.ConnectionStrings.Add(new System.Configuration.ConnectionStringSettings(
                    @"MyDatabase"
                    , @"Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=SSPI"));
                appConfig.Save();

    The connection string can be retrieved with:

    var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabase"];

    A null will be returned if the connection string is not found in the app.config and you can take the appropriate action.

     

    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    • Marked as answer by Maggie Luo Sunday, July 15, 2012 4:47 PM
    Sunday, July 8, 2012 3:05 PM
    Answerer
  • config files are the best place to store connectionstrings as per i have talked with other people. you are using the connectionstring key in the file to store, read and update info. just to know, have you tried using simple key, value to store, read and update connectionstring run time?

    http://social.msdn.microsoft.com/Forums/da-DK/csharpgeneral/thread/d68a872e-14bc-414a-82c4-d1035a11b4a8

    also, i guess there are limitation while reading, writing in registry http://stackoverflow.com/questions/53135/what-registry-access-can-you-get-without-administrator-privleges.

    regards

    joon

    Sunday, July 8, 2012 7:37 PM