Programmatically Invoking the OLEDB Data Link Config Dialog

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Environment: VC6, MDAC 2.1 ( ADO 2.1 ), Win9x / NT

Introduction

I recently needed to allow a user to have the ability to configure a connection string for one of my applications that uses ADO. After searching relentlessly through the docs, there was absolutely no information available on how to do this, except for a knowledge base article (Q225132) that described a problem with the #import statements when going from MDAC 2.0 to 2.1. This spurred me into writing this short article to demonstrate how you can use the standard OLE DB Data link configuration dialog in your own applications.

The data link dialog is contained in oledb32.dll (in MDAC 2.1, MDAC 2.0 its in msdasc.dll ). And it really is extremely easy to use. By creating an instance of the IDataSourceLocator interface, you can pop up and configure ADO data sources very quickly.

Ok, so lets take a look at how to do it. Ive created a simple console app project that demonstrates how to do this, download it by following the link at the bottom of this article.

Importing the type libraries

Firstly load up the simple source project and open the one and only source file ( datalink.cpp ) contained therein. The source is quite well commented so it should be straight forward to read it, but here are the main points.

Note from Web Master: The following #import pragmas are broken only due to formatting reasons.

#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 statements at the top of the source file tell the compiler to generate some files that allow us to use smart pointers on the data link objects and the ADO objects. These import statements will allow us to use the ADO objects, and importantly for this article the Data Link properties dialog object.

Displaying the Data Link Properties Dialog

The function main() mostly contains some code to allow us to load and save a pre-configured connection string. I'm assuming you're familiar with the ios classes, so I'll skip this part as it was only put there to demonstrate you can display the dialog based on a pre-configured connection string.

When you want to display the dialog, you simply declare a IDataSourceLocatorPtr smart pointer object ( created from the #import statements ), and create an instance of it. You are then free to call its PromptNew() and PromptEdit( IDispatch *ptr ) methods.

For example, this code segment shows how to display the dialog for creating a new connection string (error checking etc has been removed for brevity);

 oledb::IDataSourceLocatorPtr    p_IDSL = NULL;
 _ConnectionPtr                  p_conn = NULL;

 p_IDSL.CreateInstance( __uuidof( oledb::DataLinks ));

 p_conn = p_IDSL->PromptNew();

The PromptNew function will display a fresh dialog for all the parameters to be entered by the user, and upon return, p_conn will be set to either NULL, indicating the user clicked cancel, or it will contain a created Connection object with its data member ConnectionString set to the ADO connection string that represents the users choices.

To display the dialog, based upon a pre-existing connection string, its slightly more convoluted. Here's 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();

The edit version of the dialog needs these extra steps. Firstly you need to create a Connection object and set its connection string yourself, this is then converted into an IDispatch pointer using QueryInterface, and passed into the PromptEdit function.

PromptEdit returns a BOOL as TRUE if the user clicked OK, or FALSE if cancel was clicked. If it's TRUE the p_conn object's ConnectionString property will be set to the new connection parameters defined by the user.

Wrapping up

My use of these functions involved using the registry to store and retrieve the connection string. By adding this functionality to your application, you can give the user the ability to easily configure their ADO data source. I hope it helps some of you....it certainly helped me.

Downloads

Download source - 43 Kb


Comments

  • HOW TO: Build a Connection String Programmatically in ADO.NET

    Posted by Legacy on 08/01/2003 12:00am

    Originally posted by: Jose Maria Tejedor

    Just see the following: "Microsoft Knowledge Base Article - 310083".

    Reply
  • Error

    Posted by Legacy on 03/29/2003 12:00am

    Originally posted by: Brad Bruce

    p_Dispatch->Release();
    results in an error. The error code is -2147217805

    Any suggestions?

    Thanks

    Reply
  • Works in C# too

    Posted by Legacy on 01/21/2002 12:00am

    Originally posted by: Will Baldwin

    //Set a reference to the oledb32.dll and ADO
    //It will create the MSDASC Wrapper for you...

    private void PromptNew_Click(object sender, System.EventArgs e)
    {
    MSDASC.DataLinks objDataSourceDlg = new MSDASC.DataLinks();
    ADODB._Connection adoConnection;
    adoConnection = (ADODB._Connection)objDataSourceDlg.PromptNew();
    objDataSourceDlg.WriteStringToStorage(@"Connection.UDL",
    adoConnection.ConnectionString, 2 );

    //CREATE_NEW 1, CREATE_ALWAYS 2, OPEN_EXISTING 3,
    //OPEN_ALWAYS 4, TRUNCATE_EXISTING 5
    }

    private void PromptEdit_Click(object sender, System.EventArgs e)
    {
    MSDASC.DataLinks objDataSourceDlg = new MSDASC.DataLinks();
    ADODB.Connection objADOConn = new ADODB.Connection();
    object objTempConn = null;
    string strConnectionString;
    objDataSourceDlg.LoadStringFromStorage(@"Connection.UDL", out
    strConnectionString);
    objADOConn.ConnectionString = strConnectionString;
    objTempConn = (object)objADOConn;
    bool bSuccess = objDataSourceDlg.PromptEdit(ref objTempConn);
    if (bSuccess)
    {
    objDataSourceDlg.WriteStringToStorage(@"SentinelDB_Connection.UDL",
    objADOConn.ConnectionString, 2);
    }
    }

    Reply
  • Finaly found a way to use PromptEdit !!

    Posted by Legacy on 11/02/2001 12:00am

    Originally posted by: P�r Gradin

    This is the only page on the net that describes the correct way to use PromptEdit in c++. I've been searching alot of newsgroups. 
    
    

    Important thing though:
    If the connection is open when PromptEdit is used, be sure to close it before the PromptEdit call:

    if (p_conn->GetState()==adStateOpen) p_conn->Close();

    The datalinks dialog reported an error when I tried to edit my already opend connection!

    Thanx !!

    Reply
  • www.geocities.com/gemedany

    Posted by Legacy on 10/12/2001 12:00am

    Originally posted by: geme

    how can i get the password properties??(without chek "allow saving password)"

    Reply
  • Even easier way

    Posted by Legacy on 02/07/2001 12:00am

    Originally posted by: John Smith

    Use the OLE DB Consumer, use the following code

    CDataSource ds;
    ds.Open();

    2 lines of code, will open the Datalink Dialog.

    Reply
  • Storing Bitmap to Database

    Posted by Legacy on 01/23/2001 12:00am

    Originally posted by: Rajasekhar

    Hello!

    I am developing an application for PocketPC using eVC and the backend is SQLServer CE 2000 for Windows CE.The deveice database is SQLCE and using ADOCE 3.1.I am loading a bitmap into my view(CView), after that i am drawing some lines, typing some text and erasing some portion(pasting white color) on to the bmp.I want to save the frame however it's looking on the PocketPC to the dabatase.The datatype for the field on the database is image.

    I also want to read back it and display as it is in the view.Is is possible.I didn't found any way to do this.


    I want to implement WYSIWYG(What You See Is What You Get)while storing and retrieving the bitmap.

    Any help in this regard will be appreciated.

    Please email to chavasekhar@rediffmail.com

    Regards...
    Rajasekhar

    Reply
  • Storing Bitmap to Database

    Posted by Legacy on 01/23/2001 12:00am

    Originally posted by: Rajasekhar

    Hello!

    I am developing an application for PocketPC using eVC and the backend is SQLServer CE 2000 for Windows CE.The deveice database is SQLCE and using ADOCE 3.1.I am loading a bitmap into my view(CView), after that i am drawing some lines, typing some text and erasing some portion(pasting white color) on to the bmp.I want to save the frame however it's looking on the PocketPC to the dabatase.The datatype for the field on the database is image.

    I also want to read back it and display as it is in the view.Is is possible.I didn't found any way to do this.


    I want to implement WYSIWYG(What You See Is What You Get)while storing and retrieving the bitmap.

    Any help in this regard will be appreciated.

    Please email to chavasekhar@rediffmail.com

    Regards...
    Rajasekhar

    Reply
  • It's Easy in VB, too!

    Posted by Legacy on 11/28/2000 12:00am

    Originally posted by: Steve Jackson

    Here's how to display the datalinks property page using VB, in case anybody wanted to know:
    
    

    Public Sub main()
    Dim x As New MSDASC.DataLinks
    Dim y As ADODB.Connection

    Set y = x.PromptNew

    If y Is Nothing Then
    MsgBox "You clicked cancel!"
    Else
    MsgBox "Connection string: " & y.ConnectionString
    End If

    Set x = Nothing
    Set y = Nothing
    End Sub

    Reply
  • more documentation on IDataSpourceLocationPtr

    Posted by Legacy on 11/18/2000 12:00am

    Originally posted by: Shaji

    Where can I find more documentation on IDataSpourceLocationPtr, whether I can use any methods of this Interface to enumerate OLEDB datasources directly

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date