Click to See Complete Forum and Search --> : OLE DB ::Open parameters


RogerGarrett
July 25th, 2005, 05:26 PM
I'm having difficulty figuring out how to set up the parameters for doing an Open with the CDataSource class.

My database is named "MyDatabase".
It's an OLE DB type of database.
The Provider is Microsoft OLE DB Provider for SQL Server, so I think that means using the string "SQLOLEDB" as a parameter to the Open method.
My Server Name is "MyServer".

I can successully open it up using just:


CDataSource theDatabase;
hResult = theDatabase.Open();


which simply prompts me via a dialog for all the various information.

But I need to do it programmatically.

I think I need to set up a CDBPropSet with something like:


CDBPropSet dbPropertySet(DBPROPSET_DBINIT);
dbPropertySet.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
dbPropertySet.AddProperty(DBPROP_INIT_DATASOURCE, "RogesPlaceOLEDB");
dbPropertySet.AddProperty(DBPROP_INIT_PROMPT, (short)DBPROMPT_NOPROMPT);
dbPropertySet.AddProperty(DBPROP_INIT_LCID, (long)2057); // NOTE: this came from page 159 of the Visual C++ 6 DATABASE PROGRAMMING TUTORIAL book


and then open it with something like:


HRESULT hResult;
hResult = m_Database.Open(_T("SQLOLEDB"),&dbPropertySet);


but it always fails.

I'm obviously not passing in the name of the server (i.e. "MyServer") anywhere, but I can't figure out where to do that. And I suspect that some of the rest is also not quite right.

Any help would be greatly appreciated.

olivthill
July 25th, 2005, 06:54 PM
Do you know what kind of error do you get when it fails?

With a CDBPropSet filled in the way you do, you can find the following examples: http://www.codeproject.com/database/oledbconsumer1.asp, http://www.codeproject.com/database/oledbconsumer1.asp, http://www.p2p.wrox.com/archive/visual_c_plus_plus/2002-01/57.asp.

But I prefer the method where you give a connection string, with the DBPROP_INIT_PROVIDERSTRING property, like in the example at http://www.vchome.net/tech/document/database/ATLStoredProcedures.htm

You can find several examples of property strings at http://www.connectionstrings.com/