Re: Extracting an ADO ConnectString from an OLE DB provider

Michal Blazejczyk <[email protected]> Tue, 26 Nov 2002 16:07:52 -0500
Newsgroups gmane.comp.windows.devel.oledb.devel
Message-ID <[email protected]>
Hi Christer,

Here's a piece of code that does exactly what you're looking for.

It calls PromptDataSource and then uses GetInitializationString to query
the connection string.

It prompts for a new connection string. I've got another function that
does the same to edit an existing connection string.

Regards

        Michal Blazejczyk

HRESULT CDBConnectionStringParser::EditNew (HWND hParent)
{
        HRESULT                                                 hr =
E_FAIL;
        CComPtr<IDataInitialize>                pIDataInitialize;
        CComPtr<IDBPromptInitialize>    pIDBPromptInitialize;
        CComPtr<IDBInitialize>                  pIDBInitialize = NULL;
        LPOLESTR
pInitializationString = NULL;
        CString                                                 strMsg;
        WCHAR
szzFilter [] =
L"SQLOLEDB\0MSDAORA\0Microsoft.Jet.OLEDB.4.0\0OraOLEDB.Oracle\0\0";

        USES_CONVERSION;

        try
        {
                strMsg = "Internal error occured. Unable to perform
operation.\nThe selected database configuration remains unchanged.";
                HR_EXC(hr = CoCreateInstance(
                                CLSID_MSDAINITIALIZE,
// clsid--data initialize
                                NULL,
// pUnkOuter
                                CLSCTX_INPROC_SERVER,
// dwClsContext
                                IID_IDataInitialize,
// riid
                                (void**)&pIDataInitialize));    //
ppvObj

                // Create the Data Links UI object, and obtain the
                // IDBPromptInitialize interface from it.
                HR_EXC(hr = CoCreateInstance(
                                CLSID_DataLinks,
// clsid--Data Links UI
                                NULL,
// pUnkOuter
                                CLSCTX_INPROC_SERVER,
// dwClsContext
                                IID_IDBPromptInitialize,
// riid
                                reinterpret_cast<void**>(
&pIDBPromptInitialize)));// ppvObj

                // Invoke the Data Links UI to allow the user to select
                // the provider and set initialization properties for
                // the data source object that this will create.
                if (m_str_file_name.IsEmpty ())
                        strMsg = "Database configuration is
invalid.\nThe new database configuration was discarded.";
                else
                        strMsg = "Database configuration is
invalid.\nThe selected database configuration remains unchanged.";

                hr = pIDBPromptInitialize->PromptDataSource(

NULL,

hParent,

DBPROMPTOPTIONS_PROPERTYSHEET,

0,

NULL,

(LPOLESTR) szzFilter,

IID_IDBInitialize,

(IUnknown **)&pIDBInitialize );

                switch (hr)
                {
                        case S_OK:
                                break;
                        case DB_E_CANCELED:
                                strMsg = "";
                                HR_EXC (hr);
                                break;
                        default:
                                if (FAILED (hr))
                                        HR_EXC (hr);
                                break;
                }

                strMsg = "Database configuration is invalid and will be
discarded.";

                //
                // We've obtained a data source object from the Data
Links UI. This
                // object has had its initialization properties set, so
all we
                // need to do is initialize it.
                //
                HR_EXC (hr = pIDBInitialize->Initialize());

                //
                // Get the initialization string
                //
                HR_EXC(hr = pIDataInitialize->GetInitializationString(
                                pIDBInitialize,
// pDataSource
                                TRUE,
// fIncludePassword
                                &pInitializationString));
// ppwszInitString
                m_str_conn_string = OLE2T (pInitializationString);

                strMsg = "";

                parse ();
        }
        catch (_com_error&)
        {
        }

        if (pInitializationString)
                CoTaskMemFree (pInitializationString);

        if (!strMsg.IsEmpty ())
                AfxMessageBox (strMsg, MB_OK);

        return hr;
}

> -----Original Message-----
> From: Christer Ljung [mailto:[email protected]]
> Sent: Tuesday, November 26, 2002 4:00 PM
> To: [email protected]
> Subject: [OLEDB_DEV] Extracting an ADO ConnectString from an OLE DB
> provider
>
>
> I'm trying to develop a general purpose connect-to-datasource
> dialog in a C++ program.
>
> It should return a ConnectString that could be given to the
> ADO Connection object later so that
> the program can connect to arbitrary datasource through ADO
> using an OLE DB provider. The idea is
> that the user should not be an expert in typing in a
> ConnectString and could use the dialog for
> help.
>
> I did experiment with the PromptDataSource (see below) to
> launch the standard dialog, but how do I
> extract a ConnectString out of the OLE DB provider? I can get
> a lot of properties, but that leaves
> be to assemble a ConnectString on my own, and I'm back to square one.
>
>    spIDBPromptInitialize->PromptDataSource( 0, hWnd
>                                 ,
> DBPROMPTOPTIONS_PROPERTYSHEET, 0, 0, 0
>                                 , IID_IDBInitialize,
> (IUnknown**)&pIDBInitialize
>
> Is there some property that returns what I'm looking for?
>
> Best Regards,
> Christer
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
>
> You can read messages from the OLEDB_DEV archive, unsubscribe
> from OLEDB_DEV,
> or subscribe to other DevelopMentor lists at
> http://discuss.develop.com.
>

-------------------------------------------------
This communication and any files or attachments transmitted with it may
contain information that is confidential, privileged and exempt from
disclosure under applicable law or contract. It is intended solely for
the use of the individual or the entity to which it is addressed. Any
views or opinions expressed are those of the author and do not
necessarily represent those of Imaging Research Inc. If you are not the
intended recipient, you are hereby notified that any use, dissemination,
or copying of this communication is strictly prohibited. If you have
received this communication in error, please promptly notify the sender
by reply e-mail, and then destroy the original and all copies of this
transmission.

You can read messages from the OLEDB_DEV archive, unsubscribe from OLEDB_DEV,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.