5,545,318 members and growing! (17,161 online)
Email Password   helpLost your password?
Database » Database » Data Access     Intermediate

ADO.NET and UDL

By ra1234567890

Using Data Link files with SQLClient and OracleClient.
C#, VB, Windows, .NET 1.1, .NET, ADO.NET, Visual Studio, VS.NET2003, Dev

Posted: 30 Sep 2005
Updated: 30 Sep 2005
Views: 25,021
Bookmarked: 11 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.68 Rating: 3.83 out of 5
1 vote, 20.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
1 vote, 20.0%
4
2 votes, 40.0%
5

Introduction

I used to love the simplicity of making connection strings by referring to a Data Link file while using OLE DB. This was a flexible way and it reduced the hassle of making packaged installations that refers to the development server. Then ADO.NET introduced the concept of optimized connectors for both SQL Server and Oracle. To my surprise, these optimized connecters are not able to read connection string stored in the data link file. Then I had to resort to saving the connection string to a text file which can then be edited by hand whenever a change in the connection is needed. This is re-invention of the wheel. Unacceptable!

Using the code

To use the code, import the cConnection class in your project. Then create an instance of the object like this:

Friend WithEvents oConnection As New _
                  cConnection("MyDataLink.udl")

If the name of the data link file is not included, the default name is used. The data link file is created in the same directory as the calling assembly.

The next step is to pass the connection string to the connection object.

m_cnAuthors.ConnectionString = oConnection.GetSQLConnectionString()

Data Link File Creation

Some times, one forgets to package the data link file or forgets to change the connection to the appropriate server. One has to go through the agony of navigating to the installation directory and creating it in front of the customer. They don't like it. They expect the usual next, next, next ... finish way of installation. It is with this in mind that the data link file creation was included.

To create a Data Link file, create a new empty text file, change its extension to .udl and then ask the operating system to call for you the Data Link app by double clicking over the file. This is translated to:

'Create an empty text file

Dim fs As New System.IO.FileStream(strFileName, System.IO.FileMode.CreateNew)
fs.Close()


'Call Data Link App and write information to it

Dim psiProcess As New ProcessStartInfo
psiProcess.FileName = strFileName
Dim pProcess As Process = Process.Start(psiProcess)
pProcess.WaitForInputIdle()
pProcess.WaitForExit()
pProcess.Close()

The trick here is to close the filestream before starting the data link app process. Otherwise we will get errors.

Reading a Data Link file

The Data Link app writes all sorts of delights for parameters but only a few of them are essential to make a successful connection. To find all those appropriate parameters, I had to split the connection string and put it in an array. Searching had to be done in a loop as parameters are not always in a special order.

For Each strString In strArray

            If strString.IndexOf("Provider") > -1 Then
                strProvider = strString.Substring(strString.IndexOf("Provider"))
            End If

            If strString.IndexOf("Password") > -1 Then
                m_strPassword = strString
            End If
            ...
            ...
            ...
            
Next

The searched parameters are then reconstructed and passed to to the appropriate connection object.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ra1234567890



Occupation: Web Developer
Location: South Africa South Africa

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Sep 2005
Editor: Smitha Vijayan
Copyright 2005 by ra1234567890
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project