Reference: http://www.vckbase.com/index.php/wv/82
References: Baidu encyclopedia
1. How to Create a udl File
Create a TXT file, change the suffix to. udl, save it, and ignore the warning!
Double-click the udl file to bring up the udl file configuration dialog box. The configuration method is as follows:
Configure the Universal Data Link (. udl) File1. Double-click the Universal Data Link (. udl) file. The "Data Link Properties" dialog box is displayed. The following tabs are displayed: "provider", "connection", "advanced", and "all ". Select "Next" and locate in the tab. 2. on the "provider" tab, select the database provider. 3. On the "connection" tab, select the data source name (DSN) of the available provider, or enter a custom connection string. The valid DSN of the pre-defined provider in the system is displayed in the "use data source" drop-down list. 4. Use the "advanced" tab to view and set other data initialization attributes. 5. Use the "all" tab to review and edit all ole db initialization properties that can be used for the ole db Provider. 6. Select "OK" to save the connection string to the. udl file. For more information, see the "Data Link Properties" dialog box. Data Link file format Common Data Link (. the first two lines of the udl file must be exactly the same: [oledb]; everything after this line is an ole db initstring after these two required lines ,. the rest of the udl file is composed of connection strings written in the same format. This string is used when the database is opened from the programming code. The following list shows the complete text of A. udl file, which is opened using the ole db provider of Microsoft SQL Server to use the Integrated Security northwind database. [Oledb]; everything after this line is an ole db initstringprovider = sqloledb.1; persist Security info = false; Data Source = a-iresmi2000; initial catalog = northwind; Integrated Security = sspi
2. How to dynamically connect to the database in VC? The following example uses my_data1.udl to create an ADO connection.
1.
_ConnectionPtr m_pDBConn;
2.
3.
m_pDBConn.CreateInstance(__uuidof(Connection));
4.
5.
m_pDBConn->ConnectionString =
"File Name=c:\mydir\my_data1.udl"
;
6.
7.
m_pDBConn->Open(
""
,
""
,
""
,NULL);
Or directly use:
M_pconnection-> open (L "file name = D: \ Vc \ myconn. udl ", l" ", l" ",-1); (do not use this !!! An error occurred! For details, see the following error description)
In this way, you can use a unified method for programming in the software regardless of how the data source changes. When the data source changes, you only need to double-click the corresponding udl file to visually set the data source without changing the software.
The complete code is recommended as follows:
// 4. Use the ADO connection object to connect to the database
// Connection pointer
_ Connectionptr m_pconnection;
If (m_pconnection)
{
If (m_pconnection-> getstate ()! = Adstateclosed)
{
M_pconnection-> close (); // check the Mechanism
}
}
Else
{
: MessageBox (null, "error message: connection object is blank", "error", mb_iconexclamation );
Return false;
}
Try // exception Mechanism
{
M_pconnection-> connectionstring = "file name =. \ junjunconnection. udl ";
M_pconnection-> open ("", null );
}
Catch (_ com_error & E)
{
Cstring strcomerror;
Strcomerror. Format ("error no.: % 08lx \ n error message: % s \ n Error Source: % s \ n error Description: % s ",
E. Error (), // error number
E. errormessage (), // error message
(Lpcstr) E. Source (), // error source
(Lpcstr) E. Description (); // error description
: MessageBox (null, strcomerror, "pressuremonitor error", mb_iconexclamation );
Return false;
}
Return true;
3. Why can't I connect to the software when I run it according to the above method?
Error 1:
Cause of error: Your program does not find the specified udl file. Solution: 1. Confirm your udl path and actual file location; 2. Do not usem_pConnection->Open(L”File Name=D:\\vc\\MyConn.udl”,L”",L”",-1);
Error 2:
Solution: Change the absolute path to a relative path. Put your udl file in the current directory of your software, and then change the path
M_pconnection-> connectionstring = "file name =. \ junjunconnection. udl ";