Microsoft Knowledge Base Email Alertz

KBAlertz.com: (310084) - This step-by-step article describes how to use the Data Link Properties dialog box to programmatically create a connection string at design time. Requirements The following list outlines the recommended hardware, software, network infrastructure, and...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 310084 - Last Review: November 29, 2007 - Revision: 3.0

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

This article was previously published under Q310084
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://kbalertz.com/Feedback.aspx?kbNumber=840667/ ) You receive unexpected errors when using ADO and ADO MD in a .NET Framework application

On This Page

SUMMARY

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

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

  1. Start Visual Studio .NET and create a new Managed C++ Application. Name it CreateDL.
  2. Replace the default code in CreateDL.cpp with the following code:
    #include "stdafx.h"
    #include <stdio.h>
    
    #undef EOF
    #import "c:\program files\common files\system\ole db\oledb32.dll" rename_namespace("dlinks")
    #import "c:\program files\common files\system\ado\msado15.dll" rename_namespace("ado")
    
    void TestConnection(void);
    
    #ifdef _UNICODE
    int wmain(void)
    #else
    int main(void)
    #endif
    {
        TestConnection();
        return 0;
    }
    
    void TestConnection(void)
    {
    	using namespace dlinks;
    	using namespace ado;
    	
    	HRESULT hr;
       	::CoInitialize( NULL );
       	IDataSourceLocatorPtr dlPrompt = NULL;
       	_ConnectionPtr conn = NULL;
       	hr = dlPrompt.CreateInstance(__uuidof(DataLinks));
       	conn = dlPrompt->PromptNew();
       	if ( NULL != conn )
       	{
       		printf( "Connect: %s\n", (char*) conn->ConnectionString );
       	}
       
    	return;
    }
    
    					
  3. On the Debug menu, click Start without Debugging to run the project.
  4. Type the appropriate information in the Data Link Properties dialog box, and make sure that you click to select the Allow Saving Password check box.
  5. Click Test Connection.
  6. 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 create connections to ODBC datasources. Because ODBC has its own Managed provider in .NET, you receive an error if you use the Microsoft OLE DB provider for ODBC drivers option in the Data Link Properties dialog box. Additionally, you must load ADO into the application because the data link creates an ADODB Connection object that is not compatible with the OLEDBConnection object. Therefore, you must create an ADODB Connection and assign its ConnectionString property to the ConnectionString property of the OLEDBConnection object for this to work correctly.
  1. 1. Start Microsoft Visual Studio .NET, and create a new Managed C++ Application project. Name it OledbDL.cpp.
  2. Replace the default code in OledbDL.cpp with the following code:
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Data.dll>
    
    #undef EOF
    #import "c:\program files\common files\system\ole db\oledb32.dll" rename_namespace("dlinks")
    #import "c:\program files\common files\system\ado\msado15.dll" rename_namespace("ado")
    
    void TestConnection(void);
    
    #ifdef _UNICODE
    int wmain(void)
    #else
    int main(void)
    #endif
    {
        TestConnection();
        return 0;
    }
    
    void TestConnection(void)
    {
    	using namespace dlinks;
    	using namespace ado;
    	using namespace System;	
             using namespace System::Data::OleDb;
    	
    	HRESULT hr;
       	::CoInitialize( NULL );
       	IDataSourceLocatorPtr dlPrompt = NULL;
       	OleDbConnection *oleConn = new OleDbConnection();
    	_ConnectionPtr adoConn = NULL;
       	hr = dlPrompt.CreateInstance(__uuidof(DataLinks));
       	adoConn = dlPrompt->PromptNew();
    	System::String *str = (char*) adoConn->ConnectionString; // define a garbage collected string
    	oleConn->ConnectionString = str;
    
    	try
    	{
    	    oleConn->Open();
    	    if ( NULL != oleConn )
       	    {
    		Console::WriteLine("Connect: {0}\n",  oleConn->ConnectionString );
    	    }
    	}
    	catch(Exception *ex)
    	{
    		Console::WriteLine(ex->Message);
    	}
    	__finally
    	{
    		oleConn->Close();
    	}
    	return;
    }
    					
  3. On the Debug menu, click Start without Debugging to run the project.
  4. Type the appropriate information in the Data Link Properties dialog box, and make sure that you click to select the Allow Saving Password check box.
  5. Click Test Connection.
  6. 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); therefore, you cannot create a DSN-less connection through the data link.

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
286189  (http://kbalertz.com/Feedback.aspx?kbNumber=286189/EN-US/ ) HOWTO: Invoke the OLE DB Data Link Properties Dialog Box in Visual Basic Code
283245  (http://kbalertz.com/Feedback.aspx?kbNumber=283245/EN-US/ ) HOWTO: Persist Data Links Programmatically
193128  (http://kbalertz.com/Feedback.aspx?kbNumber=193128/EN-US/ ) HOWTO: Create an ODBC and OLEDB Connection Prompt Control in ADO

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
Keywords: 
kbhowtomaster KB310084
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please