Microsoft KB Archive/310083

= HOW TO: Build a Connection String Programmatically in ADO.NET by Using Visual C# .NET =

Article ID: 310083

Article Last Modified on 11/28/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft ActiveX Data Objects 2.7

-



This article was previously published under Q310083



Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:

840667 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application



For a Microsoft Visual Basic .NET version of this article, see 309485.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.OleDb

IN THIS TASK
SUMMARY
 * Requirements
 * Create an ADO Connection
 * Create an OLE DB Connection
 * Additional Information

REFERENCES



SUMMARY
This step-by-step article demonstrates how to use the Data Link Properties dialog box to programmatically create a connection string at design time.

back to the top

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

back to the top

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.  Start Visual Studio .NET, and then create a new Visual C# Windows Application project. Form1 is added to the project by default. From the Project menu, click Add Reference. On the COM tab, select the following references:  Microsoft ActiveX Data Objects 2.7 Microsoft OLEDB Service Component 1.0 Type Library

</li> If you are prompted to have a wrapper generated for you, click Yes.</li> Add a Button control to Form1.</li>  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(&quot;&quot;,&quot;&quot;,&quot;&quot;,0);

if (ADOcon.State == 1) { MessageBox.Show(&quot;Connection Opened&quot;); ADOcon.Close; } else { MessageBox.Show(&quot;Connection Failed&quot;); }                   </li> Press the F5 key to compile and run the project, and then click Button1.</li> Type the appropriate information in the Data Link Properties dialog box, and make sure that you select the Allow Saving Password check box.</li> Click Test Connection.</li> Click OK. If the connection test succeeded in the data link, a connection to the database is established, and a message box is displayed.</li></ol>

back to the top

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. <ol> Start Visual Studio .NET, and then create a Visual C# Windows Application project. Form1 is added to the project by default.</li> From the Project menu, click Add Reference.</li> On the COM tab, select the following references:  Microsoft ActiveX Data Objects 2.7</li> Microsoft OLEDB Service Component 1.0 Type Library</li></ul>

</li> Add a Button control to Form1.</li>  Add the following code to the top of the Code window: using System.Data.OleDb; </li>  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 == &quot;Open&quot;) { MessageBox.Show(&quot;Connection Opened&quot;); OleCon.Close; } else { MessageBox.Show(&quot;Connection Failed&quot;); }                   </li> <li>Press F5 to compile and run the project, and then click Button1.</li> <li>Type the appropriate information in the Data Link Properties dialog box, and make sure that you select the Allow Saving Password check box.</li> <li>Click Test Connection.</li> <li>Click OK. If the connection test succeeded in the data link, a connection to the database is established, and a message box is displayed.</li></ol>

back to the top

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.

back to the top

<div class="references_section">