2

我有这个 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>

我正在尝试使用此 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();   
        } 

但没有进行更新,

 using System.Configuration;

在标题和参考中,有什么问题??????

我写错了问题,我想将连接字符串存储在某个地方,可能是 app.config 或其他文件,并在另一个安装或服务器或用户更改时更改字符串。这样做的正确方法是什么???

4

1 回答 1

5

好吧,您的问题很可能是您正在查看错误的文件。查看config.FilePath调试器中的属性 - 它会准确地告诉您此时此地正在处理的文件。

当您在 Visual Studio 中运行此代码时,您的代码将更改Yourapplication.exe.config您的project\bin\debug目录内部 - 它不会更改app.config您的项目目录中的内容!

所以当你运行你的代码时——在 之后config.Save(),看看你的项目\bin\debug目录和里面的配置文件——这些值是否正确更新了?

于 2013-07-31T20:41:33.577 回答