• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

Conn String failed to OLEDB in C++

Hi Expert

i am C# programmer and i have to use C++ this time, see if you can help.
i download  an example for fast load from MSDN, and the connection string failed.
it failed after Initialized and open dos window without say what the error is:

 // Setup the initialization options
   ULONG cProperties = 0;
   DBPROP rgProperties[10];
   ULONG cPropSets = 0;
   DBPROPSET rgPropSets[1];
   LPWSTR pwszProgID = L"SQLOLEDB";
   LPWSTR pwszDataSource = L"SPEL_531_01_Copy";
   LPWSTR pwszUserID = L"sa";
   LPWSTR pwszPassword = L"Password.1";
   LPWSTR pwszProviderString =L"server=isr-x-gavi;integrated security=SSPI;";


   IDBInitialize * pIDBInitialize = NULL;
   IDBCreateSession * pIDBCrtSess = NULL;
   IOpenRowset * pIOpenRowset = NULL;
   IDBCreateCommand * pIDBCrtCmd = NULL;
   ICommandText * pICmdText = NULL;
   IAccessor * pIAccessor = NULL;
   IRowsetFastLoad * pIRowsetFastLoad = NULL;
   IDBProperties * pIDBProperties = NULL;
   DBBINDING rgBinding[COL_NUM];
   DBBINDSTATUS rgStatus[COL_NUM];
   ULONG ulOffset = 0;
   HACCESSOR hAcc = DB_NULL_HACCESSOR;
   BYTE * pData = NULL;
   ULONG iRow = 0;
   LPWSTR pwszTableName = L"IRFLTable";
   DBID TableID;

   HRESULT hr;

// DBPROP_INIT_DATASOURCE
   if(pwszDataSource)
   {
      rgProperties[cProperties].dwPropertyID    = DBPROP_INIT_DATASOURCE;
      rgProperties[cProperties].dwOptions       = DBPROPOPTIONS_REQUIRED;
      rgProperties[cProperties].dwStatus        = DBPROPSTATUS_OK;
      rgProperties[cProperties].colid           = DB_NULLID;
      rgProperties[cProperties].vValue.vt       = VT_BSTR;
      V_BSTR(&rgProperties[cProperties].vValue) = SysAllocString(pwszDataSource);              
      cProperties++;
   }

   // DBPROP_AUTH_USERID
   if(pwszUserID)
   {
      rgProperties[cProperties].dwPropertyID    = DBPROP_AUTH_USERID;
      rgProperties[cProperties].dwOptions       = DBPROPOPTIONS_REQUIRED;
      rgProperties[cProperties].dwStatus        = DBPROPSTATUS_OK;
      rgProperties[cProperties].colid           = DB_NULLID;
      rgProperties[cProperties].vValue.vt       = VT_BSTR;
      V_BSTR(&rgProperties[cProperties].vValue) = SysAllocString(pwszUserID);
      cProperties++;
   }

   // DBPROP_AUTH_PASSWORD
   if(pwszPassword)
   {
      rgProperties[cProperties].dwPropertyID    = DBPROP_AUTH_PASSWORD;
      rgProperties[cProperties].dwOptions       = DBPROPOPTIONS_REQUIRED;
      rgProperties[cProperties].dwStatus        = DBPROPSTATUS_OK;
      rgProperties[cProperties].colid           = DB_NULLID;
      rgProperties[cProperties].vValue.vt       = VT_BSTR;
      V_BSTR(&rgProperties[cProperties].vValue) = SysAllocString(pwszPassword);
      cProperties++;
   }

   // DBPROP_INIT_PROVIDERSTRING
   if(pwszProviderString)
   {
      rgProperties[cProperties].dwPropertyID    = DBPROP_INIT_PROVIDERSTRING;
      rgProperties[cProperties].dwOptions       = DBPROPOPTIONS_REQUIRED;
      rgProperties[cProperties].dwStatus        = DBPROPSTATUS_OK;
      rgProperties[cProperties].colid           = DB_NULLID;
      rgProperties[cProperties].vValue.vt       = VT_BSTR;
      V_BSTR(&rgProperties[cProperties].vValue) = SysAllocString(pwszProviderString);
      cProperties++;
   }

   if(cProperties)
   {
      rgPropSets[cPropSets].cProperties = cProperties;
      rgPropSets[cPropSets].rgProperties = rgProperties;
      rgPropSets[cPropSets].guidPropertySet = DBPROPSET_DBINIT;
      cPropSets++;
   }

   // Initialize
   if (SUCCEEDED(hr))
      hr = pIDBProperties->SetProperties(cPropSets, rgPropSets);
   if (SUCCEEDED(hr))
      hr = pIDBInitialize->Initialize();
   if (SUCCEEDED(hr))
   {
      printf("\tConnected!\r\n");
   }
   else
      printf("Unable to connect\r\n");


sorry for the long code...

10x
0
gudidi
Asked:
gudidi
  • 3
  • 2
1 Solution
 
ikeworkCommented:
There are some problems with your code:

1) You dereference the pointer pIDBProperties, but it has a NULL-value, not pointing to anything:

  IDBProperties * pIDBProperties = NULL;
  ...
  hr = pIDBProperties->SetProperties(cPropSets, rgPropSets);

2) The same for pIDBInitialize:

  IDBInitialize * pIDBInitialize = NULL;
  ...
  hr = pIDBInitialize->Initialize();

3) You check "hr" without setting it to anything in the first place:

    HRESULT hr;
    ... hr is not set to anything, it has a random value
    if (SUCCEEDED(hr))

  Usually it is used like this:

    hr = AnyFunction();
    if (SUCCEEDED(hr))


ike
0
 
ikeworkCommented:
I guess you used this code:


  http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/ffec3dae-980f-4ebb-9fd2-c671ba1fcf26
You removed some important initialisations from the beginning of the code
0
 
gudidiAuthor Commented:
hi

sorry to budde you but it is like chnese for me...
can you try to tell me how to fix?
i am not C++ programmer
0
 
gudidiAuthor Commented:
hi i have initialized did sent it before:

 // Set init props
   for ( ULONG i = 0 ; i < sizeof(rgProperties) / sizeof(rgProperties[0]) ; i++ )
      VariantInit(&rgProperties[i].vValue);

   // Obtain the provider's clsid
   CLSID clsidProv;
   hr = CLSIDFromProgID(pwszProgID, &clsidProv);

   // Get our initial connection
   CoInitialize(NULL);

   if (SUCCEEDED(hr))
      hr = CoCreateInstance(clsidProv, NULL, CLSCTX_ALL, IID_IDBInitialize,(void **)&pIDBInitialize);

   if (SUCCEEDED(hr))
      hr = pIDBInitialize->QueryInterface(IID_IDBProperties, (void **)&pIDBProperties);

and it goe's ok until the line i fall.

0
 
gudidiAuthor Commented:
hi i use the eamle in the link and it works


10x a lot
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now