Article ID: 310083 - Last Review: November 28, 2007 - Revision: 4.2 HOW TO: Build a Connection String Programmatically in ADO.NET by Using Visual C# .NETThis 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
(http://support.microsoft.com/kb/840667/
)
You receive unexpected errors when using ADO and ADO MD in a .NET Framework application On This PageSUMMARY
This step-by-step article demonstrates how to use the Data Link Properties dialog box to programmatically create a connection string at design time. RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
Create an ADO ConnectionFor 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.
Create an OLE DB ConnectionWhen 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.
Additional InformationIt 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.REFERENCESFor additional information, click the article numbers below
to view the articles in the Microsoft Knowledge Base:
286189
(http://support.microsoft.com/kb/286189/EN-US/
)
HOWTO: Invoke the OLE DB Data Link Properties Dialog Box in Visual Basic Code
283245
(http://support.microsoft.com/kb/283245/EN-US/
)
HOWTO: Persist Data Links Programmatically
193128
(http://support.microsoft.com/kb/193128/EN-US/
)
HOWTO: Create an ODBC and OLEDB Connection Prompt Control in ADO
| Article Translations
|
Back to the top
