Original author: Tony Johnson and Chris Wilcock
The translator: This article mainly talks about how to use the OLEDB's Data Connection Properties dialog box, depending on the user's choice, use the dynamically obtained connection string to configure the data source of the ADO.
The OLEDB's Data Connection Configuration dialog is shown below:
Program compilation environment: VC6, MDAC 2.1 (ADO 2.1), Win9X / NT
Introduction Introduction
I have recently needed to allow users to configure a connection string according to their needs. After searching for the recent documentation, I found that there is no information about how to implement this. In addition to the transition from MDAC2.0 to 2.1, the article (Q225132) describes a question that uses #import declarations in the Knowledge Base. This stimulates me to write an article to demonstrate how to make you use the OLE DB data connection properties dialog box in your application.
This data connection dialog is included in OLEDB32.DLL (in MDAC2.1, MDAC2.1 in MSDasc.dll). He is very easy to use. By creating an iDataSourceLocator interface instance, you can pop up this dialog and configure the ADO data source.
Ok, let's take a look at how to achieve him? I have created a simple console program engineering to demonstrate how to do this, at the end of the article, can download this project file by connecting.
Enter Type Library Importing The Type Libraries
First, load the project and open a source file (Datalink.cpp). In this source file, I have already commented on the statement, so you can read it easily. But there is still some key points.
#import "C: / Program Files / Common Files / System / ADO"
"/msado20.tlb" NO_NAMESPACE RENAME ("EOF", "ISEOF")
Rename ("Bof", "Isbof")
#import "C: / Program Files / Common Files / System / OLE"
DB / OLEDB32.DLL "RENAME_NAMESPACE (" OLEDB ")
The #import declaration statement at the top of the header file will tell the compiler to generate some files so that we use the data connection and the intelligent pointer of the ADO object. These input declaration statements also allow us to use ADO objects, most importantly for the Data Connection Properties dialog boxes mentioned herein.
Display Data Connection Properties dialog Displaying The Data Link Properties Dialog
The main () function contains some code that the role of these codes is to allow us to load or save a predefined connection string. I assume that you have been familiar with the IO class, so I will have this part, this part is just demonstrated to use a predefined connection string when displaying this Data Connection Properties dialog.
For example, if the following code segment tells you how to display this dialog box to create a new connection string (error check has been removed)
OLEDB :: IdatasourceLocatorptr p_idsl = null;
_ConnectionPtr p_conn = null;
p_idsl.createInstance (__UUIDOF (OLEDB :: Datalinks);
p_conn = p_idsl-> proptnew ();
The function of the PromptNew function is to display a new dialog box for users to enter all parameters. When returned, p_conn can be null, which is the user clicking the cancel button, or a CONNECTION) Objects, according to the user's choice, the data member CONNNECTIONSTRING of this object is set to the ADO connection string. To display a connection string that already exists, this is a bit of a puzzle. Below is an example.
OLEDB :: IdatasourceLocatorptr p_idsl = null;
_ConnectionPtr p_conn = null;
p_idsl.createInstance (__UUIDOF (OLEDB :: Datalinks);
p_conn.createInstance ("AdoDb.Connection");
p_conn-> connectionstring = _bstr_t ("Your connection string");
Idispatch * p_dispatch = null;
p_conn.queryinterface (IID_IDISPATCH, (LPVOID *) & p_dispatch);
p_idsl-> pRomptedit (& P_DISPATCH);
p_dispatch-> release ();
There are additional steps that can be edited. First, you need to create a connection object and set its connection string, and then translate into an IDispatch pointer using queryinterface and finally passed to the Promptit function.
PROMPTEDIT returns a BOOL value, if the user clicks OK, return true, otherwise returns false. If you are True, the Connectionstring property of the P_Conn object will be set to the new user-defined connection parameters.
caveat
I use these functions including the use of registry to store and re-connect. Add these functions to your application, you can easily configure their ADO data sources. I hope he will help you ... he is indeed helpful to me.
Downloads
Download Source - 43 KB