Share via


Edit ConnectionString in dll.config file

Question

Wednesday, October 9, 2013 9:39 PM

We have compiled a program and want to be able to change the connection string in the dll.config file.  So that if a server changes or we need to update the password we don't have to recompile the code.  When I edit the dll.config file it seems to ignore what changes I made unless I go into visual studios and change and recompile it.  

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="T3KAdmin_L3.Properties.Settings.T3KConnString" connectionString="Data Source=DataSourceName;Initial Catalog=CatalogName;Persist Security Info=True;User ID=sa;Password=Password"            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

SqlConnection Conn = new SqlConnection();
                string myConnString = T3KAdmin_L3.Properties.Settings.Default.T3KConnString;
                     Conn = new SqlConnection(myConnString);
                Conn.Open();

                SqlCommand cmd = Conn.CreateCommand();
                cmd.CommandText = String.Format("Exec {0} {1}", strStoredProc, strParams);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                Conn.Close();
                return ds;

All replies (5)

Friday, October 11, 2013 5:33 AM ✅Answered

Hi BrittonW,

We could not be as simple to use as the ASP.NET Configuration File. Please follow the below steps to achieve your goal.

  1. Add a reference to System.Configuration.
  2. Add a new Configuration File and rename it to your project.dll.config. If your application’s name is T3KAdmin_L3.exe, then rename the file to T3KAdmin_L3.exe.config.
  3. Change the BuildAction property of the config file to “Content”.
  4. Change the Copy to Output property to “Copy Always”.

Use the following code to read from config file.

public class Test

    {

        public string GetConfig()

        {

            //Open the configuration file using the dll location

            Configuration myDllConfig =

                   ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);

            // Get the appSettings section

            AppSettingsSection myDllConfigAppSettings =

                   (AppSettingsSection)myDllConfig.GetSection("appSettings");

            // return the desired field

            return myDllConfigAppSettings.Settings["ConnectionString"].Value;

        }

}

Your configuration file like below:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="ConnectionString" value="xxxxxxxxxxx"/>

  </appSettings>

</configuration>

Best Regards,

Hetro

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Friday, October 11, 2013 6:48 AM ✅Answered | 1 vote

<copied>

We have compiled a program and want to be able to change the connection string in the dll.config file.  So that if a server changes or we need to update the password we don't have to recompile the code.  When I edit the dll.config file it seems to ignore what changes I made unless I go into visual studios and change and recompile it.  

<end>

Why is the connection string information not setting in the root app.config or Web.config where .NET is looking for configuration information?


Wednesday, October 23, 2013 6:45 PM

it is in the App.config but I can't find this file to edit it after the release I can only find the connectionstring in the T3KAdmin_L3.dll.config file


Wednesday, October 23, 2013 7:48 PM

<copied>

it is in the App.config but I can't find this file to edit it after the release I can only find the connectionstring in the T3KAdmin_L3.dll.config file

<end>

T3KAdmin_L3.dll.config file .NET can't be possibly looking in that file. It's not the root.config for the solution. If it's a Web application, then the connection string should be in the root Web.config. If this is an exe type project, then the connection string should be in the App.config of that project. When the project is built, the App.config information in the project is copied to a file named "programname.exe.config" that sits next the programname.exe. The programname.exe.config must be deployed to the location of the programname.exe so that .NET can find it. This dll.config is questionable to say the least that .NET is even using the file.


Thursday, October 24, 2013 2:26 AM

Hi Darnold,

Yes, I agree with you. If you can, please still use the exe configuration file. However, if the situation is not allowed, then the above code is useful.

Best Regards,

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.