WinForm中如何获取连接字符串,并保存修改。加载时读取出保存的连接字符串,显示到文本框中。
点击测试连接按钮时,使用输入的连接字符串进行服务器连接尝试,弹出尝试结果信息。
点击确定按钮时将输入的连接字符串保存

解决方案 »

  1.   

    把链接字符串保存到app.config中
    读取的时候从app.config读出字符串。保存时,将字符串保存到app.config中
      

  2.   

    读取配置文件,你的问题就是一个如何读取写入xml的问题
      

  3.   


    System.Xml.XmlDocument domSysinfo=new System.Xml.XmlDocument();
    System.Xml.XmlNode root;
    System.Xml.XmlNode node;
    System.Xml.XmlNodeList nodeList; domSysinfo.Load(AppDomain.CurrentDomain.BaseDirectory + "\\app.config");                  
    root=domSysinfo.SelectSingleNode("//configuration"); 
    node=root.SelectSingleNode("//appSettings");
    nodeList=node.SelectNodes("add");
    for(int i=0;i<nodeList.Count;i++)
    {
    if(nodeList[i].Attributes["key"].InnerText.Equals("ConnStr"))//讀 {
    nodeList[i].Attributes["value"].InnerText="寫";
    }
    }
    domSysinfo.Save(AppDomain.CurrentDomain.BaseDirectory + "\\app.config");
      

  4.   


    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <add key="ConnStr" value="連接串" />
    </appSettings>
    </configuration>
    4樓對應的app.config文件內容。注意,編譯後app.config可能會改名為編譯後的可執行文件名+.config
      

  5.   

    你的WindowsApplication项目下有个Properties文件夹,下面有个Settings.settings文件,双击进去,新建一个值,类型选择连接字符串
    读的时候:
       string connString = Properties.Settings.Default.ConnString;
    写的时候
     Properties.Settings.Default.ConnString=“”;
     Properties.Settings.Defaul.Save();
      

  6.   

    用如下的代码可以解决读和更改数据库连接字符串的问题。
    ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings;
    foreach (ConnectionStringSettings s in connectionStrings)
    {
      //更改ConnectionString...
    }
      

  7.   

    可以这样
    首先在应用程序里添加配置文件
    app.config file:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <!--   User application and configured property settings go here.-->
    <!--   Example: <add key="settingName" value="settingValue"/> -->
    <add key="ServerIP" value="127.0.0.1"/>
    <add key="Server" value="test_temp"></add>
    <add key="user" value="sa"></add>
    <add key="password" value="sa"></add>
    </appSettings>
    </configuration>数据连接字符串,可以读取到当前app.config中的数据库连接信息:

    string strcon=String.Format ("packet size=4096;data source={0};persist security info=True;initial catalog={1};user id={2};password={3}",ConfigurationSettings.AppSettings["SQLserverIP"],ConfigurationSettings.AppSetting["Server"],ConfigurationSettings.AppSetting["user"],ConfigurationSettings.AppSettings["password"]);按搂主的意思可以做一config窗体,上面需有IP,Database,user,password等组成的字符串,然后可以动态修改。
    比如我们加几个textbox,对应相关数据库连接信息如下:txtIP1.Text:ServerIP//服务器
    txtSvrName.Text:Database//数据库实例(名称)
    txtName1.Text:user//数据库用户
    txtPassword1.Text:password//密码如果修改连接信息,直接在textbox上修改就可以,那需要写个方法:
    首先引用了
    using System.Xml;
    using System.Configuration;
    命名空间private void SaveConfig(string ConnenctionString,string strKey)
    {
    XmlDocument doc=new XmlDocument();
    //获得配置文件的全路径
    string  strFileName=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
    doc.Load(strFileName);
    //找出名称为“add”的所有元素
    XmlNodeList nodes=doc.GetElementsByTagName("add");
    for(int i=0;i<nodes.Count;i++)
    {
    //获得将当前元素的key属性
    XmlAttribute att=nodes[i].Attributes["key"];
    //根据元素的第一个属性来判断当前的元素是不是目标元素
    if (att.Value==strKey) 
    {
    //对目标元素中的第二个属性赋值
    att=nodes[i].Attributes["value"];
    att.Value=ConnenctionString;
    break;
    }


    }
    //保存上面的修改
    doc.Save(strFileName);
    }再修改保存设置时调用SaveConfig()方法:private void btnOK_Click(object sender, System.EventArgs e)
    {
    if (txtIP1.Text=="")
    {
    MessageBox.Show("ServerIP is not allow null");
    return ;
    }
    else if(txtSvrName.Text=="")
    {
    MessageBox.Show("DataBase is not allow null");
    return ;
    }
    else if(txtName1.Text=="")
    {
    MessageBox.Show("User Name is not allow null");
    return ;
    }
    else
    {
    SaveConfig(txtIP1.Text,"ServerIP");
    SaveConfig(txtSvrName.Text,"Server");
    SaveConfig(txtName1.Text,"user");
    SaveConfig(txtPassword1.Text,"password");
    MessageBox.Show("Config Sucessful!","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
    }
    this.Close();

    }
    注意你程序目录下,程序运行时应该是修改在当前域ConfigurationFile
    bin/debug/AutoRun.exe.config
    //4楼方法完全可以的,
      

  8.   

    回复9楼
    按照你说的可以读取的出连接字符串
    但是写入不进去
    Properties.Settings.Default.ConnString是只读的属性
      

  9.   


    app.exe.config
    app.vshost.exe.config这二者有什么区别?
    修改的是哪一个呢?楼上有几位修改的好像稍有不同
      

  10.   

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
    当前应用程序域配置信息
    就是你的应用
    bin/debug/程序名字.exe.config 文件信息
      

  11.   

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 
    获得的是程序名字.vshost.exe.config文件
      

  12.   

    vshost.exe.config文件从哪里来的?真不清楚,我这边只有可执行exeName.exe.config,我这边做测试都ok的
    命苦啊。没分,
      

  13.   

    像你所说,2005里有宿主进程.vshost.exe,需要手动禁用。
    vs2003里不需要
      

  14.   

    错误 2 “System.Xml.XmlNodeList”并不包含“Attributes”的定义 D:\My Documents\Visual Studio 2005\Projects\YT_JXC.root\YT_JXC_MSSQL\YT_JXC\Spring\Tools.cs 274 33 YT_JXC_MSSQL