Microsoft KB Archive/308075

= How to use Data Link files with the OleDbConnection object in Visual Basic .NET =

Article ID: 308075

Article Last Modified on 12/28/2005

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q308075





For a Microsoft Visual C# .NET version of this article, see 308426.



For a Microsoft Visual Basic 6.0 version of this article, see 189680.

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

IN THIS TASK
SUMMARY
 * Steps to Build the Sample

REFERENCES



SUMMARY
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 Visual Basic .NET Console Application 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 more information about how to create a .udl file, click the following article number to view the article in the Microsoft Knowledge Base:

189680 How to use Data Link files with ADO

NOTE: When you create the .udl file, make sure to select the Allow Saving Password option on the Connection tab. Test the connection from the .udl file. Open a new Visual Basic .NET Console Application.  Replace the default code in the Module1 code module with the following code: Imports System.Data.OleDb Imports Console = System.Console

Module Module1

Sub Main

Dim response As String

Dim cn As New OleDbConnection cn.ConnectionString = &quot;File Name=c:\test.udl&quot; cn.Open If cn.State = ConnectionState.Open Then Console.WriteLine(&quot;Connection opened successfully&quot;) Else Console.WriteLine(&quot;Connection could not be established&quot;) End If       cn.Close cn = Nothing

response = Console.ReadLine

End Sub

End Module  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). A console window should open and display the text, &quot;Connection opened successfully!&quot; to indicate that the OleDbConnection object was able to use the connection string information in the .udl file to establish the database connection.</li> Press any key to dismiss the console window and stop the application.</li></ol>

back to the top

<div class="references_section">