Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Persist Data Links Programmatically


View products that this article applies to.

This article was previously published under Q283245

↑ Back to the top


Summary

This article explains how to persist and use Microsoft Data Links programmatically with Microsoft Visual Basic and Microsoft Visual C++.

↑ Back to the top


More information

Data Links provide a convenient way to create and verify a connection string that can be used to connect to an OLE DB data source. The connection string information is persisted in a specific format in a Data Link file with the .udl extension. The functionality is similar to the Open Database Connectivity (ODBC) file Data Source Names (DSNs), but .udl files contain Unicode characters.

The following is the content of a sample Data Link file. You can edit this file using a Unicode text editor such as Microsoft Notepad.
[oledb]
; Everything after this line is an OLE DB initstring.
Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=myServer

					
The following code shows how to prompt the user to get the connection information in Visual Basic 6.0 using Data Links. To use this code, set project references to the Microsoft ActiveX Data Objects 2.x Library and the Microsoft OLE DB Service Component 1.0 Type Library. When you use Visual C++ 6.0, the CDataSource Active Template Library (ATL) class provides similar functionality.
    Dim cnn As ADODB.Connection
    Dim dlk As MSDASC.DataLinks
   
    Set cnn = New ADODB.Connection
    Set dlk = New MSDASC.DataLinks
    
    cnn.ConnectionString = dlk.PromptNew
    cnn.Open
				
There are two ways to persist Data Link files programmatically:
  • Use the Shell application programming interface (API) to invoke the .udl files so that the connection information can be obtained from the user and then persisted automatically. The following C++ sample code creates a new Data Link file, and then opens the file using the ShellExecute function. This invokes the Data Link Properties dialog box, where the user can enter connection information which is then persisted into the .udl file.
    	/* Create and open a new udl file. */ 
    	FILE* file;
    	if( (file = fopen( "c:\\new.udl", "w" )) == NULL )
    		/* handle the error here */;
    	else 
    		/* Close the file */ 
    		fclose(file);
    
    	/* Prompt the user with Data Links to enter connection information. */ 
    	ShellExecute(NULL, "open", "c:\\new.udl", NULL, NULL, SW_SHOWNORMAL);
    					
  • Persist the file to the disk using file APIs or classes. In Visual C++, you can use standard file input/output (I/O) APIs such as iostream classes of C++ or CFile and its derived classes of the Microsoft Foundation Class (MFC) library.

    In Visual Basic, to open a file for binary access, you can use the following syntax for the Open statement:
    Open pathname For Binary As filename
    Note that the characters in the binary file should be in Unicode format. Alternatively, the file can be written in text format and then converted into Unicode format. The content should be similar in format to the sample .udl file displayed above (for example, the first two lines should be the same as shown and the third line should contain the required name/value pairs). The keywords used in the connection string may be specific to the OLE DB provider that is used.

↑ Back to the top


References

For more information, see "Using the Data Link API" at the following Microsoft Web site: For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
189680� How To Use Data Link Files with ADO
195913� How To Generate ODBC and OLEDB Connection Strings with Data Links
244659� SAMPLE: How to Create a Data Link File with Windows 2000
218600� How To Use Data Links to Create a Connection String at Run-Time

↑ Back to the top


Keywords: KB283245, kbhowto, kbdatabase, kbcodesnippet

↑ Back to the top

Article Info
Article ID : 283245
Revision : 5
Created on : 7/1/2004
Published on : 7/1/2004
Exists online : False
Views : 311