在代码中调用系统数据源配置

工作的需要,要在自己的程序里调用数据源配置,以便用户灵活配置数据源,查了很多资料,正统的解决方案(不用自己做控件,调用代码极少,灵活方便)就是调用WIN系统的DataConnection 对话框了,之前在国内网上苦找的代码,终于有两个方案可用,但是后来发现是有缺陷的,今天在国外网站看到一个办法,其实和前面两个方案之一是一样的,加了一条语句(MSDN上查不到),这一条关键语句的扩充,和那个方案取长补短,终于可是实现比较完美的数据源配置了;

方案1:

l         ADO.NET OLEDB ODBC

1.     添加两个dll引用,包括一个.Netadodb.dll和一个COMoledb32.dllMicrosoft Ole DB Service Component 1.0 Type Library)。
2.
引入两个命名空间:adodb.dllADODBoledb32.dllMSDASC
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的引用,这个assemblyVS2005的安装目录下,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;
               }
           }
       }
}

说明:此方案初期认为不可行,因为错误认为它只可配置ODBC数据源,后来发现是可以配置ADO.NET的(OLE DB),但是在代码里无法添加上“其它数据库”选项,导致无法扩展OLE DB,其默认的可在代码添加的数据源只有ACCESS,SQLserver,SQLserver文件,Orcal和ODBC五种
方案3:( 最终解决方案
国外网站参考出处: http://www.thescripts.com/forum/thread677730.html
方案3其实是在方案2的基础上加一行代码,
DataSource.AddStandardDataSources(dialog);
之前发现AddStandrdDataSources方法,但是没找到帮助,且无法添加,原来就是如此简单

说明:选择“<其他>”后,下拉框的OLE DB数据源提供程序,下一步,则像方案一那样所有的OLE DB程序都出现了
附录:国外网站原文:
lildiapaz's Avatar
lildiapaz
Newbie
23 Posts
July 15th, 2007
11:47 PM
# 1

dataConnection dialog in c#
Hi, everyone

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

转载于:https://www.cnblogs.com/srliuhao/articles/1178253.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值