问题描述:

I have this app.config :

<?xml version="1.0"?>

<configuration>

<configSections>

</configSections>

<connectionStrings>

<add name="conexx" connectionString="Data Source=192.168.1.2 ;Initial Catalog =ifdcontroladoria3 ;uid =sa;pwd = admin2012" providerName="System.Data.SqlClient" />

</connectionStrings>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>

I m trying to update this connectring with this C# :

 coneydstr = @"Data Source=" + comboBox1.Text + ";Initial Catalog =" + cmbBancos.Text + ";uid =" + txtUsuario.Text + ";pwd =" + txtPassword.Text;

try

{ coneyd.ConnectionString = coneydstr;

coneyd.Open();

funciona = true;

lblStringSalida.Text = coneydstr;

}

catch (Exception ex)

{ MessageBox.Show(coneyd + ex.Message.ToString()); }

if (funciona)

{

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.ConnectionStrings.ConnectionStrings["conexx"].ConnectionString = coneydstr;

config.Save();

}

but is not making update ,

 using System.Configuration;

is in the header and the reference , what is wrong?????

I have write wrong the problem, i want to store the connectionstring somewhere maybe app.config or other file , and change the string in another install or when then server or user changes. What the right way to do this???

网友答案:

Well, your problem most likely is that you're looking at the wrong file. Look at the config.FilePath property in the debugger - it will tell you exactly which file you're dealing with here and now.

When you run this code in Visual Studio, your code will change the Yourapplication.exe.config inside your project\bin\debug directory - it will NOT change the app.config in your project directory!

So when you run your code - after the config.Save(), have a look at your project's \bin\debug directory and the config file inside there - are those values properly updated?

相关阅读:
Top