How to: In Visual C # .NET, the data link file is used for OLEDBConnection objects.

xiaoxiao2021-03-06  73

The release number of this article has been CHS308426

For Microsoft Visual Basic .NET versions of this article, see

308075.

For Microsoft Visual C .NET versions of this article, see

308428.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.Data.Oledb

This task content

summary

Steps to generate an example

Summary This article demonstrates how to use an OLE DB Universal Data Link (.UDL) file to specify ADO.NET

OLEDBConnection objects are used to create a database connection connection string.

The OLE DB Universal Data Link (.UDL) file provides a convenient method for creating and verifying the ActiveX data object (ADO) connection string for connecting to the OLE DB data source. Connection string information is kept in a file in a specific format in an extension .udl. You can be in ADO.NET

OLEDBConnection object

A .udl file is referenced in the Connectionstring property to specify the connection string that the object should be used when connecting to a database. Use the connection string

File name parameters to specify the name of the .udl file containing this connection string information.

Note The following information is very important, this information is derived from the "OLEDBConnection.connectionstring property" article in the .NET Framework Class library document (see

Reference section):

To reference Microsoft Data Link (UDL), add a reference to UDL to "File Name = MyFile.udl". Add to Connectionstring. If multiple UDLs are specified in the connection string, only the last item is loaded. If the OLEDBConnection object is called after specifying the UDL to retrieve Connectionstring, the connectionString contains references to the UDL file without containing the contents of the file. An connection string containing UDL is analyzed each time the OLEDBConnection is turned on. Because this will affect performance, it is recommended to use Connectionstring that does not quote UDL.

Back to top

To generate an example, follow these steps to create a Visual C # .NET console application, the application uses the .udl file as ado.net

OLEDBConnection object Specifies the connection string information:

Use the SQL Server OLE DB provider to connect to a Microsoft SQL Server database, and then create a .udl file called Test.udl in the root directory of the C drive. You can also configure the .udl file using the Microsoft OLE DB Provider for Jet 4.0 to connect to the Microsoft Access 97 or 2000 database when you cannot access the SQL Server database. For additional information about how to create a .udl file, click the following article number, view article in the Microsoft Knowledge Base: 189680 HOWTO: USE DATA LINK Files with ado (HOWTO: Use Data Link File for ADO) Open New Visual C # .NET console application. Replace the default code in the Class1 module to the following code: USING SYSTEM;

Using system.data;

Using system.data.oledb;

Namespace Datalink

{

Class class1

{

Static void main (string [] args)

{

OLEDBCONNECONNECONNECTION = New OLEDBCONNECTION ("File Name = D: //Test.udl"); TRY

{

MyConnection.open ();

IF (MyConnection.State == ConnectionsState.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;

}

}

} In the statement for setting the Connectionstring property of the OLEDBConnection object, if you create a .udl file or save it to another location, please modify the path of the file. Run the application from the Visual Studio .NET integration development environment (IDE). You should open a console window and display "Connection Opened Success" (the connection has been successfully opened!) Text, indicating that the OLEDBConnection object can establish a database connection using the connection string information in the .udl file. Press ENTER to turn off the console window and stop the application.

Back to top

Reference

For more information, please refer to the following .NET Framework Class library documentation:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbConnectionClassConnectionStringTopic.asp For additional information, click the following article number to view the article in the Microsoft Knowledge Base :

189680 HOWTO: USE DATA LINK FILES with ADO (HOWTO: Data Link File for ADO)

Back to top

The information in this article applies to:

Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)

Reserved: 2002-6-17 (1.0) Keyword Kbhowto KbhowTomaster Kboledb KbsystemData KB308426

转载请注明原文地址:https://www.9cbs.com/read-109002.html

New Post(0)