using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Linq;
using System.Xml.Linq;
兄弟们,我做winform程序,我在app.config里定了一个数据库连接的key.
想通过客户端输入组成数据库连接字符串并修改app.config的key值.
系统提示保存成功,但是实际上则没有修改,什么原因呢.
下面是app.config文件代码:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="SqlString" value=""/>
  </appSettings>
</configuration>
下面是程序代码如下:
namespace winACCESSORY
{
    public partial class serverSetup : Form
    {
        public serverSetup()
        {
            InitializeComponent();
        }        private void smtButton_Click(object sender, EventArgs e)
        {
            string ConnectString = "server=" + txtIP.Text.ToString().Trim() + ";database=" + txtDataBaseName.Text + ";user id=" + txtName.Text + ";password=" + txtPassword.Text;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection app = config.AppSettings;
            try
            {                
               app.Settings["SqlString"].Value = ConnectString;                  
               config.Save(ConfigurationSaveMode.Modified);
                MessageBox.Show("配置保存成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }             
        }
        private void button3_Click(object sender, EventArgs e)
        {
            string ConnectString = "server=" + txtIP.Text.ToString().Trim() + ";database=" + txtDataBaseName.Text + ";user id=" + txtName.Text + ";password=" + txtPassword.Text;
            SqlConnection conn = new SqlConnection(ConnectString);
            try
            {
                conn.Open();
                if (conn.State == System.Data.ConnectionState.Open)
                {
                    MessageBox.Show("数据库连接测试成功!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
      
    }
}

解决方案 »

  1.   

    这段代码为修改config键值代码
    private void smtButton_Click(object sender, EventArgs e)
      {
      string ConnectString = "server=" + txtIP.Text.ToString().Trim() + ";database=" + txtDataBaseName.Text + ";user id=" + txtName.Text + ";password=" + txtPassword.Text;
      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      AppSettingsSection app = config.AppSettings;
      try
      {   
      app.Settings["SqlString"].Value = ConnectString;   
      config.Save(ConfigurationSaveMode.Modified);
      MessageBox.Show("配置保存成功!");
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }   
      }
      

  2.   

    你修改的可能是DEBUG里的文件吧
      

  3.   


    同意。可能就是每次重编译,导致app.config被覆盖。
      

  4.   


    XmlDocument xdoc = new XmlDocument();
                AppDomain ad = AppDomain.CurrentDomain;
                string ConnectString = "server=" + txtIP.Text.ToString().Trim() + ";database=" + txtDataBaseName.Text + ";user id=" + txtName.Text + ";password=" + txtPassword.Text;
                string key = "SqlString";            xdoc.Load(ad.SetupInformation.ConfigurationFile);            xdoc.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value = ConnectString;
                xdoc.Save(Ad.SetupInformation.ConfigurationFile);
    试试看
      

  5.   

    更多信息查看资料:
    http://www.cnblogs.com/xshy3412/archive/2007/11/24/971374.html
      

  6.   

    系统提示成功是你弹出的messagebox而已,前面不抛异常,就弹出来了,怎能当真?这样试试:
    Configuration config = ConfigurationManager.OpenExeConfiguration("MyWinform.exe");
    请改为你的程序最终的名称.