none
introduce values into app.config RRS feed

  • Question

  • Hello

    I have a question about app.config and c#

    I have a program in C# where I can connect to sql and get some information from my database.

    I use app.config to connect into the database, please follow code below:

     

    <connectionStrings>
        <add name="sip" connectionString="Data Source=JSALAZAR\SQLEXPRESS;Initial Catalog=Sipelia;Integrated Security=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    however, this is static. It will be the same instance name and database. I am looking some option to introduce dynamic data according to the instance name and database name.

    How can I use some string variables to introduce a different database and instances name in case I need to conenct with a different server?

    Regards,

    Jose

    Wednesday, November 1, 2017 1:47 AM

All replies

  • Hello Jose,

    >>How can I use some string variables to introduce a different database and instances name in case I need to conenct with a different server?

    You could achieve it by using Configuration class ,which could add, remove, update, select node information in your config file.

     public static void SettingConfig(string Serverinstance, string DatabaseName)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                string ConnStr = string.Format("Data Source = {0}; Initial Catalog = {1}; Integrated Security = True", Serverinstance, DatabaseName);
                config.ConnectionStrings.ConnectionStrings["sip"].ConnectionString = ConnStr;
    
                config.Save(ConfigurationSaveMode.Modified, true);
            }
     

    Note. you will find the modified value in your .exe.config file under debug folder . As for why not app config you could see here.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 1, 2017 11:47 AM
    Moderator
  • By default your application will be stored in a path where your users won't have write access to the app files (including your config). Furthermore if you ever update your app then the config changes will be lost.

    If you need to store small sets of data then use the Registry or create a document in the user's Documents folder. This means you cannot use the ConfigurationManager code but your app should be generally contract-based anyway so it shouldn't matter.

    If you need to store larger sets of data and you really want to use ConfigurationManager then you can use per-user settings instead. They basically act the same but are stored as a config file in the user's local directory so it can be read/written. Of course going this route introduces issues when you need to upgrade your app.

    MSDN has some guidelines on these approaches.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Fei HuModerator Thursday, November 2, 2017 10:08 AM
    Wednesday, November 1, 2017 2:46 PM
    Moderator
  • Hello ,

    Is there anything updated? If the issues has been solved, you could close the thread by marking  as answer, This can be beneficial to other community members reading this thread.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 15, 2017 10:57 AM
    Moderator