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/
--> XmlDocumentmyDoc = new XmlDocument();
XmlElementmyXmlElement;
myDoc.Load(Application.ExecutablePath + " .config " );
XmlNodemyNode = 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/
--> XmlDocumentmyDoc = new XmlDocument();
XmlElementmyXmlElement;
myDoc.Load(Application.ExecutablePath + " .config " );
XmlNodemyNode = 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/
--> <? xmlversion="1.0"encoding="utf-8" ?>
< configuration >
< configSections >
</ configSections >
< connectionStrings >
< add name ="NXY.Properties.Settings.nxyInfoConnectionString" connectionString ="DataSource=SC440;InitialCatalog=nxyInfo;PersistSecurityInfo=True;UserID=sa;Password=sa"
providerName ="System.Data.SqlClient" />
</ connectionStrings >
</ configuration >
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> <? xmlversion="1.0"encoding="utf-8" ?>
< configuration >
< configSections >
</ configSections >
< connectionStrings >
< add name ="NXY.Properties.Settings.nxyInfoConnectionString" connectionString ="DataSource=SC440;InitialCatalog=nxyInfo;PersistSecurityInfo=True;UserID=sa;Password=sa"
providerName ="System.Data.SqlClient" />
</ connectionStrings >
</ configuration >