310083 HOW TO: Use Visual C # .NET to programmatically build connection strings in ADO.NET (from mkba)

xiaoxiao2021-03-06  74

The release number of this article has been CHS310083

For Microsoft Visual Basic .NET versions of this article, see

309485.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.Data.Oledb

This task content

summary

Create an ADO connection to create an OLE DB connection additional information

Summary This step-by-step guidance article demonstrates how to use

The Data Link Properties dialog box creates a connection string in programming.

Back to top

Require the following list lists the recommended hardware, software, network structure, and service pack required:

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 Basics and Syntax ActiveX Data Objects (ADO) Basics and Syntax

Back to top

Creating an ADO connection To implement interoperability with the Component Object Model (COM), you must use ADO

Connection object declaration

AdoDB._Connection, and enforce the general object returned by the data link to

AdoDB._Connection.

Start Visual Studio .NET and create a Visual C # Windows application project. The Form1 is added to the project by default. From the project menu, click Add Reference. On the COM tab, choose the following quote:

Microsoft ActiveX Data Objects 2.7 Microsoft OLEDB Service Component 1.0 Type Library If prompted to generate a package, click Yes. Add a button control to FORM1 to add. Add the following code to the Button1_Click event: msdasc.datalinks mydlg = new msdasc.datalinks ();

Adodb._connection adocon;

// Cast The Generic Object That Promptnew Returns to an adodb._connection.

Adocon = (adoDb._connection) mydlg.promptnew ();

Adocon.open (",", ",", 0);

IF (adocon.state == 1) {

MessageBox.show ("Connection Opened");

Adocon.Close ();

}

Else {

Messagebox.show ("Connection Failed");

} Press F5 to compile and run the project, and then click Button1. Type the appropriate information in the Data Link Properties dialog box and make sure the "Allow Save Password" check box is selected. Click Test Connection. Click OK. If the connection test of the data link is successful, connect to the database and display a message box.

Back to top

Creating OLE DB Connections When you create an OLE DB connection with the OLE DB managed provider that uses .NET, you cannot use the data link to create a connection to the ODBC data source. Because ODBC has its own hosted provider in .NET, if you are

Use in the Data Link Properties dialog box

The Microsoft OLEDB Provider options for the ODBC driver will receive an error. In addition, because the data link creates one with

OLEDBCONNECTION object is not compatible with object (this object is forced to convert to AdoDB._Connection), you must load ADO to the application. Therefore, you must create one

AdoDb._connection and it

Connectionstring property specifies

OLEDBConnection object

The Connectionstring property makes it working properly.

Start Visual Studio .NET and create a Visual C # Windows application project. The Form1 is added to the project by default. From the project menu, click Add Reference. On the COM tab, choose the following quote:

Microsoft ActiveX Data Objects 2.7 Microsoft OLEDB Service Component 1.0 Type Library adds a Button control to Form1. Add the following code to the top of the "Code" window: use system.data.oledb; Add the following code to the Button1_Click event: msdasc.datalinks mydlg = new msdasc.datalinks ();

OLEDBCONNECON = 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");

} Press F5 to compile and run the project, and then click Button1. Type the appropriate information in the Data Link Properties dialog box and make sure the "Allow Save Password" check box is selected. Click Test Connection. Click OK. If the connection test of the data link is successful, connect to the database and display a message box.

Back to top

Other information Use this method to create an ODBC connection require some job, because the connection string created by the data link is specific to OLE DB, which is not compatible with the ODBC hosting provider. To do this, information on the ADODB connection string must be analyzed, such as user ID, password, and data sources. Once this information is obtained, you can use this information to create an ODBC-specific connection string. Remember the Data Link Only use the ODBC Data Source Name (DSN), so you cannot create a DSN connection through a data link.

Back to top

Refer to other information, click the following article number to view the appropriate Microsoft Knowledge Base article:

286189 HOWTO: INVOKE THE DB DATA LINK Properties Dialog Box in Visual Basic Code (HOWTO) Call OLE DB Data Link Properties dialog box in Visual Basic Code)

283245 HOWTO: PERSIST DATA LINKS PROGRAMAATILY (HOWTO: Save Data Links) 193128 HOWTO: CREATE AN ODBC AND OLEDB Connection PROMPT Control In Ado (Howto: Create an ODBC and OLEDB Connection Tips Control in ADO)

Back to top

The information in this article applies to:

Microsoft ADO.NET (included in the .NET Framework) Microsoft Visual C # .NET (2002) ActiveX Data Object (ADO) 2.7

Recent Update: 2002-6-18 (1.0) Keyword Kbadonet Kbcominterop Kbdatabase Kbhowto KbhowTomaster Kboledb KbsystemData KB310083

转载请注明原文地址:https://www.9cbs.com/read-108910.html

New Post(0)