Microsoft KB Archive/283245

= How To Persist Data Links Programmatically =

Article ID: 283245

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Data Access Components 2.0
 * Microsoft Data Access Components 2.1
 * Microsoft Data Access Components 2.5
 * Microsoft Data Access Components 2.6
 * Microsoft Data Access Components 2.7

-



This article was previously published under Q283245



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



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] Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=myServer
 * Everything after this line is an OLE DB initstring.

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( &quot;c:\\new.udl&quot;, &quot;w&quot; )) == NULL ) /* handle the error here */; else /* Close the file */ fclose(file);

/* Prompt the user with Data Links to enter connection information. */    ShellExecute(NULL, &quot;open&quot;, &quot;c:\\new.udl&quot;, 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.

