Is it possible (and how) to add a ConnectionString to app.config at runtime in C #?

I have a console application that receives a connectionstring parameter as a parameter. I need to set the ConnectionString in app.config with the name "ConnectionString" and the given parameter as the sql connection string.

thanks to the answers. Using the links I got to this:

var config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None); var connectionStringSettings = new ConnectionStringSettings ("ConnectionString", _arguments ["connectionString"], "System.Data.SqlClient"); config.ConnectionStrings.ConnectionStrings.Add (connectionStringSettings); config.Save (ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection ("ConnectionStrings");

+5
source share
2 answers

PLAY THIS ANSWER AND SEE BELOW.

Yes - look at this one .

This method is probably you after

System.Configuration.ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings("new name", "new string");
-6
source

, // app.config . .

using System.Configuration; // don't forget to add the system.configuration dll to your references.

    public static void CreateConnectionString(string datasource, string initialCatalog, string userId, string password)
    {
        try
        {
            //Integrated security will be off if either UserID or Password is supplied
            var integratedSecurity = string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(password);

            //Create the connection string using the connection builder
            var connectionBuilder = new SqlConnectionStringBuilder
            {
                DataSource = datasource,
                InitialCatalog = initialCatalog,
                UserID = userId,
                Password = password,
                IntegratedSecurity = integratedSecurity
            };

            //Open the app.config for modification
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //Retreive connection string setting
            var connectionString = config.ConnectionStrings.ConnectionStrings["ConnectionStringName"];
            if (connectionString == null)
            {
                //Create connection string if it doesn't exist
                config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings
                {
                    Name = ConnectionName,
                    ConnectionString = connectionBuilder.ConnectionString,
                    ProviderName = "System.Data.SqlClient" //Depends on the provider, this is for SQL Server
                });
            }
            else
            {
                //Only modify the connection string if it does exist
                connectionString.ConnectionString = connectionBuilder.ConnectionString;
            }

            //Save changes in the app.config
            config.Save(ConfigurationSaveMode.Modified);
        }
        catch (Exception)
        {
            //TODO: Handle exception
        }
}

, .

+10

All Articles