Data Connection Dialog (直接调用内置数据源连接对话框)

本文介绍如何在WinForm应用程序中使用VS2005配置界面处理数据库连接字符串的更改,并通过示例代码展示了如何实现数据库连接字符串的动态配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

winform程序,在发布时往往需要更改数据库连接字符串,而数据库采用附加的方式配置数据库连接字符串,可以使用VS2005的配置界面来处理

引用C:\Program Files\Microsoft Visual Studio 8\Common7\IDE 里面的Microsoft.Data.ConnectionUI.Dialog.dll。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->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);
}

}

}


为了保存数据库字符串,我们需要更改VS的配置文档(程序的connectString在配置文件内)。.exe文件的配置文件为.exe.config;在同一目录下

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->XmlDocumentmyDoc=newXmlDocument();
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文件

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<addname="NXY.Properties.Settings.nxyInfoConnectionString"connectionString="DataSource=SC440;InitialCatalog=nxyInfo;PersistSecurityInfo=True;UserID=sa;Password=sa"
providerName
="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值