How to: Use Visual C # .NET to program a connection string in ADO.NET
Article ID: 310083 Last Update Date: June 18, 2002 Version: 1.0
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.OLDB
This task content
•summary
• Requirements • Create an ADO connection • Create an OLE DB connection • Other information • Reference
This page
Summary reference
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 Object (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.
1. Start Visual Studio .NET, then create a Visual C # Windows application project. The Form1 is added to the project by default. 2. From the project menu, click Add Reference. 3. On the COM tab, choose the following quote:
• Microsoft ActiveX Data Objects 2.7 • Microsoft OLEDB Service Component 1.0 Type Library 4. If prompted to generate a package, click Yes. 5. Add a button control to Form1 to add. 6. 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");
} 7. Press F5 to compile and run the project, then click Button 1.8. Type the appropriate information in the Data Link Properties dialog box and make sure the "Allow Save Password" check box is selected. 9. Click Test Connection. 10. 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 use it 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 your application. Therefore, you must create one
AdoDb._connection and it
Connectionstring property specifies
OLEDBConnection object
The Connectionstring property makes it working properly.
1. Start Visual Studio .NET and create a Visual C # Windows application project. The Form1 is added to the project by default. 2. From the project menu, click Add Reference. 3. On the COM tab, choose the following quote:
• Microsoft ActiveX Data Objects 2.7 • Microsoft OLEDB Service Component 1.0 Type Library4. Add a button control to Form1.5. Add the following code to the top of the "Code" window: use system.data.oledb; 6. Add the following code Add To 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");
} 7. Press F5 to compile and run the project, then click Button 1.8. Type the appropriate information in the Data Link Properties dialog box and make sure the "Allow Save Password" check box is selected. 9. Click Test Connection. 10. 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
reference
For additional information, click the following article number, view the appropriate Microsoft Knowledge Base article: 286189 HOWTO: INVOKE The OLE DB DATA LINK Properties Dialog Box in Visual Basic Code (HOWTO: Calling OLE DB Data Link Properties Dialog frame)
283245 HOWTO: PERSIST DATA LINKS Programmatical (Howto: Save Data Links)
193128 HOWTO: CREATE AN ODBC AND OLEDB Connection Prompt Control in ado (HOWTO: Create ODBC and OLEDB Connection Tips Controls in ADO)