winform程序,在发布时往往需要更改数据库连接字符串,而数据库采用附加的方式配置数据库连接字符串,可以使用VS2005的配置界面来处理
引用C:/Program Files/Microsoft Visual Studio 8/Common7/IDE 里面的Microsoft.Data.ConnectionUI.Dialog.dll。
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using Microsoft.Data.ConnectionUI;
private void button1_Click(object sender, EventArgs e)
{
DataConnectionDialog dia=new DataConnectionDialog ();
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using Microsoft.Data.ConnectionUI;
private void button1_Click(object sender, EventArgs e)
{
DataConnectionDialog dia=new DataConnectionDialog ();
dia.DataSources.Add(DataSource.SqlDataSource);
dia.SelectedDataProvider = DataProvider.SqlDataProvider;
if (DataConnectionDialog.Show(dia, this) == DialogResult.OK )
{
string myConnect = dia.ConnectionString;
MessageBox.Show(myConnect);
}
}
}
dia.SelectedDataProvider = DataProvider.SqlDataProvider;
if (DataConnectionDialog.Show(dia, this) == DialogResult.OK )
{
string myConnect = dia.ConnectionString;
MessageBox.Show(myConnect);
}
}
}
为了保存数据库字符串,我们需要更改VS的配置文档(程序的connectString在配置文件内)。.exe文件的配置文件为.exe.config;在同一目录下
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->XmlDocument myDoc = new XmlDocument();
XmlElement myXmlElement;
myDoc.Load(Application.ExecutablePath + ".config");
XmlNode myNode = myDoc.SelectSingleNode("//connectionStrings");
myXmlElement = (XmlElement)myNode.SelectSingleNode("//add [@name='NXY.Properties.Settings.nxyInfoConnectionString']");
myXmlElement.SetAttribute("connectionString", myConnect);
myDoc.Save(Application.ExecutablePath + ".config");
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->XmlDocument myDoc = new XmlDocument();
XmlElement myXmlElement;
myDoc.Load(Application.ExecutablePath + ".config");
XmlNode myNode = myDoc.SelectSingleNode("//connectionStrings");
myXmlElement = (XmlElement)myNode.SelectSingleNode("//add [@name='NXY.Properties.Settings.nxyInfoConnectionString']");
myXmlElement.SetAttribute("connectionString", myConnect);
myDoc.Save(Application.ExecutablePath + ".config");
相对应的XML文件
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="NXY.Properties.Settings.nxyInfoConnectionString" connectionString="Data Source=SC440;Initial Catalog=nxyInfo;Persist Security Info=True;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="NXY.Properties.Settings.nxyInfoConnectionString" connectionString="Data Source=SC440;Initial Catalog=nxyInfo;Persist Security Info=True;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
本文转自yonghu86 51CTO博客,原文链接:http://blog.51cto.com/yonghu/1321398,如需转载请自行联系原作者