在代码中调用系统数据源配置
工作的需要,要在自己的程序里调用数据源配置,以便用户灵活配置数据源,查了很多资料,正统的解决方案(不用自己做控件,调用代码极少,灵活方便)就是调用WIN系统的DataConnection 对话框了,之前在国内网上苦找的代码,终于有两个方案可用,但是后来发现是有缺陷的,今天在国外网站看到一个办法,其实和前面两个方案之一是一样的,加了一条语句(MSDN上查不到),这一条关键语句的扩充,和那个方案取长补短,终于可是实现比较完美的数据源配置了;
方案1:
l ADO.NET 、OLEDB、 ODBC
1. 添加两个dll引用,包括一个.Net库adodb.dll和一个COM库oledb32.dll(Microsoft Ole DB Service Component 1.0 Type Library)。
2. 引入两个命名空间:adodb.dll的ADODB和oledb32.dll的MSDASC。
3. 下面的方法能打开“数据链接属性”对话框,并把用户的设置作为链接字符串返回:
publicstring GetConnectionString()
{
string strConn = string.Empty;
MSDASC.DataLinks dataLink = new MSDASC.DataLinksClass();
ADODB.Connection myConn = dataLink.PromptNew() as ADODB.Connection ;
strConn = myConn.ConnectionString;
return strConn;
}
说明:此方案初期觉得很好,数据源很多,OLEDB全部涵盖,还有OLEDB支持的ODBC,但后来实际编码发现,对ODBC支持有问题,用OLEDB Provider for ODBC连接返回的字符串里是“Data Source=”,虽然理论上和“DSN=”一样,但是在C#代码里用OdbcConnection连接不上,只有改为DSN才可以,虽然可以截取字符串替换为DSN,但是不直接,并不是最佳方案;
方案2:
1. 首先添加对Microsoft.Data.ConnectionUI.Dialog.dll的引用,这个assembly在VS2005的安装目录下,C:"Program Files"Microsoft Visual Studio 8"Common7"IDE下边,命名空间引入 Microsoft.Data.ConnectionUI
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
namespace ConnectionBuilder
{
public partial class ConnectionForm : Form
{
public ConnectionForm()
{
InitializeComponent();
}
privatevoid btnCreateConnection(object sender, EventArgs e)
{
DataConnectionDialog dialog = new DataConnectionDialog();
//添加数据源列表,可以向窗口中添加自己程序所需要的数据源类型
dialog.DataSources.Add(DataSource.SqlDataSource);
dialog.DataSources.Add(DataSource.OdbcDataSource);
dialog.DataSources.Add(DataSource.OracleDataSource);
dialog.DataSources.Add(DataSource.SqlFileDataSource);
dialog.DataSources.Add(DataSource.AccessDataSource);
dialog.SelectedDataSource = DataSource.OdbcDataSource;
dialog.SelectedDataProvider = DataProvider.OdbcDataProvider;
//只能够通过DataConnectionDialog类的静态方法Show出对话框
//不能使用dialog.Show()或dialog.ShowDialog()来呈现对话框
if (DataConnectionDialog.Show(dialog, this) == DialogResult.OK)
{
txtConnectionString.Text = dialog.ConnectionString;
}
}
}
}
国外网站参考出处:http://www.thescripts.com/forum/thread677730.html
![]() |
dataConnection dialog in c#
I'm using the microsoft.connectionUI.dialog reference in a c# windows application.
Is there anyway to connect to the database using the data.connectionUI dialog. What I want to happen in my c# windows app, is the user selects sql server type from list, then the provides their authentication. When they provide their authentication is there a way to connect to that database they provide and list all the tables within that database and manipulate the tables if I need to in c#?
Also, is the dataconnection UI freeware? is it available for any programmer to use in their program? Are there any copyrights law that prevent programmers from using this in their code?
code:
DataConnectionDialog dcd = new DataConnectionDialog();
//allows you to add datasources, if you want to specify which will be supported
//dcd.DataSources.Add(DataSource.SqlDataSource);
//dcd.DataSources.Add(DataSource.SqlFileDataSource);
//Adds all the standard supported databases
DataSource.AddStandardDataSources(dcd);
DataConnectionDialog.Show(dcd);
MessageBox.Show("Connectionstring: "+dcd.ConnectionString + ""nSelected Data Source: " + dcd.SelectedDataSource.DisplayName + ""nSelected Data Provider: "+ dcd.SelectedDataProvider.DisplayName);
Any help will be appreciated