How To Use Data Link Files with the OleDbConnection Object in Visual C++ .NET (308428)
The information in this article applies to:
- Microsoft ADO.NET (included with the .NET Framework 1.1)
- Microsoft ADO.NET (included with the .NET Framework) 1.0
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
This article was previously published under Q308428 For a Microsoft Visual Basic .NET version of this
article, see
308075. For a Microsoft Visual C#
.NET version of this article, see
308426. This article refers
to the following Microsoft .NET Framework Class Library namespace:
IN THIS TASKSUMMARY This article demonstrates how to use an OLE DB Universal
Data Link (.udl) file to specify the connection string that an ADO.NET OleDbConnection object uses to establish a database connection. OLE DB
Universal Data Link (.udl) files provide a convenient way to create and verify
an ActiveX Data Objects (ADO) connection string that is used to connect to an
OLE DB data source. The connection string information is persisted in a
specific format to a file with the .udl extension. You can reference a .udl
file in the ConnectionString property of an ADO.NET OleDbConnection object to specify the connection string that the object should
use to connect to a database. Use the File Name parameter of the connection string to specify the name of the
.udl file that contains the connection string information.
back to the top
Steps to Build the Sample Follow these steps to create a Managed C++ Application project
that uses a .udl file to specify the connection string information for an
ADO.NET OleDbConnection object:
- Use the SQL Server OLE DB Provider to connect to one of
your Microsoft SQL Server databases, and create a .udl file named Test.udl in
the root folder of drive C.
You can also use the Microsoft OLE DB
Provider for Jet 4.0 to configure the .udl file to connect to a Microsoft
Access 97 or 2000 database if you do not have access to a SQL Server
database.For additional information about how to create a .udl file,
click the article number below to view the article in the Microsoft Knowledge
Base: 189680 How To Use Data Link Files with ADO
- Open a new Managed C++ Application project.
- In Solution Explorer, double-click the .cpp
file.
- Replace the default code in the source (.cpp) file with the
following code:
#include "stdafx.h"
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
using namespace System;
using namespace System::Data;
using namespace System::Data::OleDb;
// This is the entry point for this application.
#ifdef _UNICODE
int wmain(void)
#else
int main(void)
#endif
{
String *constr = "File Name=c:\\test.udl";
OleDbConnection *myConnection = new OleDbConnection(constr);
try
{
myConnection->Open();
if (myConnection->State == ConnectionState::Open)
Console::WriteLine("Connection opened successfully");
else
Console::WriteLine("Connection could not be established");
}
catch(Exception *ex)
{
Console::Write(ex->Message);
}
__finally
{
myConnection->Close();
}
return 0;
}
- In the statement that sets the ConnectionString property of the OleDbConnection object, modify the path to the .udl file if you created it with a
different name or persisted it to a different location.
- Run the application from within the Visual Studio .NET
Integrated Development Environment (IDE). Success or failure is indicated in
the console window.
- Press any key to dismiss the console window and stop the
application.
back to the top
REFERENCESFor additional
information, click the article number below to view the article in the
Microsoft Knowledge Base: 244659 SAMPLE: How to Create a Data Link File with Windows 2000
back to the top
Modification Type: | Minor | Last Reviewed: | 7/15/2004 |
---|
Keywords: | kbHOWTOmaster kbSystemData KB308428 kbAudDeveloper |
---|
|