在软件中使用“连接字符串(ConnectionString)生成向导对话框”方法大全

一、通过udl文件建立(可集成于软件中)

新建一个文本文件,将扩展名改为“udl”,在window下双击,即可弹出向导。

一个 ADO 连接中使用在代码中的数据链接文件

数据链接文件用于 ADO 连接是相当简单。 以下是在使用绝对路径的语法:

cnPubs.ConnectionString = "File Name=C:/Path/To/Pubs.UDL;"

 

二、VS中ConnectionUI.Dialog

首先添加对Microsoft.Data.ConnectionUI.Dialog.dll的引用,这个assembly在VS2005的安装目录下,C:/Program Files/Microsoft Visual Studio 8/Common7/IDE下边,下面是代码:

 

三、Data Link Properties dialog box

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:

  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
  • Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:

  • Visual Studio .NET
  • ADO.NET fundamentals and syntax
  • ActiveX Data Objects (ADO) fundamentals and syntax

Create an ADO Connection

For Component Object Model (COM) interoperability, you must declare the ADO Connection object as ADODB._Connection and cast the generic object that the data link returns to an ADODB._Connection.

  1. Start Visual Studio .NET, and then create a new Visual C# Windows Application project. Form1 is added to the project by default.
  2. From the Project menu, click Add Reference.
  3. On the COM tab, select the following references:
    • Microsoft ActiveX Data Objects 2.7
    • Microsoft OLEDB Service Component 1.0 Type Library
  4. If you are prompted to have a wrapper generated for you, click Yes.
  5. Add a Button control to Form1.
  6. Add the following code to the Button1_Click event:
  7. Press the F5 key to compile and run the project, and then click Button1.
  8. Type the appropriate information in the Data Link Properties dialog box, and make sure that you select the Allow Saving Password check box.
  9. Click Test Connection.
  10. Click OK. If the connection test succeeded in the data link, a connection to the database is established, and a message box is displayed.

  

Create an OLE DB Connection

When you create an OLE DB connection with the OLE DB managed provider in .NET, you cannot use the data link to create connections to ODBC data sources. Because ODBC has its own managed provider in .NET, you receive an error if you use the Microsoft OLEDB provider for ODBC drivers option in the Data Link Properties dialog box. In addition, you must load ADO into the application because the data link creates an object, which you cast to an ADODB._Connection, that is not compatible with the OleDbConnection object. Thus, you must create an ADODB._Connection and assign its ConnectionString property to the ConnectionString property of the OleDbConnection object for this to work properly.

  1. Start Visual Studio .NET, and then create a Visual C# Windows Application project. Form1 is added to the project by default.
  2. From the Project menu, click Add Reference.
  3. On the COM tab, select the following references:
    • Microsoft ActiveX Data Objects 2.7
    • Microsoft OLEDB Service Component 1.0 Type Library
  4. Add a Button control to Form1.
  5. Add the following code to the top of the Code window:
    using System.Data.OleDb;
    					
  6. Add the following code in the Button1_Click event:
    MSDASC.DataLinks mydlg = new MSDASC.DataLinks();
    OleDbConnection OleCon = new OleDbConnection();
    ADODB._Connection ADOcon;
    
    //Cast the generic object that PromptNew returns to an ADODB._Connection.
    ADOcon = (ADODB._Connection) mydlg.PromptNew();
    
    OleCon.ConnectionString = ADOcon.ConnectionString;
    OleCon.Open();
    
    if (OleCon.State.ToString() == "Open") {
    	MessageBox.Show("Connection Opened");
    	OleCon.Close();
    }
    else {
    	MessageBox.Show("Connection Failed");
    }
    					
  7. Press F5 to compile and run the project, and then click Button1.
  8. Type the appropriate information in the Data Link Properties dialog box, and make sure that you select the Allow Saving Password check box.
  9. Click Test Connection.
  10. Click OK. If the connection test succeeded in the data link, a connection to the database is established, and a message box is displayed

Additional Information

It requires additional effort to use this method to create an ODBC connection because the data link creates a connection string that is specific to OLE DB and is not compatible with the ODBC managed provider. For this to work, you must parse the ADODB connection string for the relevant information such as the user ID, password, and data source. After you obtain this information, you can use it to create a connection string that is specific to ODBC. Keep in mind that the data link only uses ODBC data source names (DSNs); thus, you cannot create a DSN-less connection through the data link.(src:http://support.microsoft.com/default.aspx?scid=kb;EN-US;310083

 

四、VC建立 ADO 连接字符串对话框的例子

第一步:引入dll

#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename "EOF","adoEOF")

第二步:这里给出两个 建立ADO连接字符串对话框 的例子。
datalinks.cpp 建立和编辑ADO连接字符串
linktest.cpp 如果只是建立连接字符串,程序还可以这么写。

 

再贴一个实例:

请下载源代码,结合代码可以更清楚源代码1:数据库连接演示

需要注意的是,在建立工程时,要选上Automation选项,

在StdAfx.h中加入下面代码

#import "C:/program files/common files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF")
//如果使用 ADO 2.0 加入下面代码
#import "C:/Program Files/Common Files/System/Ole DB/msdasc.dll" no_namespace 
//如果使用 ADO 2.1 加入下面代码
#import "C:/Program Files/Common Files/System/Ole DB/oledb32.dll" no_namespace
(src:http://www.vckbase.com/document/viewdoc/?id=755
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值