How To Use Data Link Files with the OleDbConnection Object in Visual C# .NET

Article translations Article translations
Article ID: 308426 - View products that this article applies to.
This article was previously published under Q308426
Expand all | Collapse all

On This Page

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.

It is important to note the following information, which is taken from the "OleDbConnection.ConnectionString Property" article in the .NET Framework Class Library documentation (see the References section):
To reference a Microsoft Data Link (UDL), add a reference to the UDL to the ConnectionString in the form "File Name = myfile.udl". If more than one UDL is specified in the connection string, only the last occurrence is loaded. If you specify a UDL, and subsequently call the OleDbConnection object to retrieve the ConnectionString, the ConnectionString contains the reference to the UDL file, not the contents of the file. Connection strings that contain UDLs are parsed each time the OleDbConnection is opened. Because this can affect performance, a ConnectionString that does not reference a UDL is recommended.

Steps to Build the Sample

Follow these steps to create a Visual C# .NET Console Application that uses a .udl file to specify the connection string information for an ADO.NET OleDbConnection object:
  1. 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
  2. Open a new Visual C# .NET Console Application.
  3. Replace the default code in the Class1 module with the following code:
    using System;
    using System.Data;
    using System.Data.OleDb;
    
    namespace DataLink
    {
        class Class1
        {
            static void Main(string[] args)
            {
                OleDbConnection myConnection = new OleDbConnection("File Name = d:\\test.udl");
    
                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.WriteLine(ex.Message.ToString());
                }
                finally
                {
                    myConnection.Close();
                }
                Console.ReadLine();
     
                return;
            }
        }
    }
    					
  4. 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.
  5. Run the application from within the Visual Studio.NET Integrated Development Environment (IDE). A console window should open and display the text, "Connection opened successfully!" to indicate that the OleDbConnection object was able to use connection string information in the .udl file to establish the database connection.
  6. Press ENTER to dismiss the console window and stop the application.

REFERENCES

For more information about the OleDbConnection.ConnectionString property, see the following .NET Framework Class Library documentation:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.connectionstring.aspx
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
189680 How To Use Data Link Files with ADO

Properties

Article ID: 308426 - Last Review: July 14, 2004 - Revision: 3.4
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 kbsystemdata KB308426

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com