For a Microsoft Visual Basic .NET version of this
article, see
309485.
For a Microsoft Visual C#
.NET version of this article, see
310083.
This article
refers to the following Microsoft .NET Framework Class Library namespace:
IN THIS TASK
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.
back to the topRequirements
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
- Start Visual Studio .NET and create a new Managed C++
Application. Name it CreateDL.
- 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;
}
- On the Debug menu, click Start without Debugging to run the project.
- 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.
- Click Test Connection.
- Click OK. If the connection test succeeded in the data link, a connection
to the database is established, and a message box is displayed.
back to the topCreate 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. Start Microsoft Visual Studio .NET, and create a new
Managed C++ Application project. Name it OledbDL.cpp.
- 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;
}
- On the Debug menu, click Start without Debugging to run the project.
- 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.
- Click Test Connection.
- Click OK. If the connection test succeeded in the data link, a connection
to the database is established, and a message box is displayed.
back to the topAdditional 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.
back to the topREFERENCES
For additional
information, click the article numbers below to view the articles in the
Microsoft Knowledge Base:
286189 HOWTO: Invoke the OLE DB Data Link Properties Dialog Box in Visual Basic Code
283245 HOWTO: Persist Data Links Programmatically
193128 HOWTO: Create an ODBC and OLEDB Connection Prompt Control in ADO
back to the top