![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I know this issue has been solved before for some people. I looked all over the net and found some solutions but they do not work for me. I am fairly new to SQL. I am not using stored procedures and due to schedule, do not have time to go down a learning path for that. I am using MFC and ADO. I can add the records (with AddNew) just fine. That works. I am stuck! Thanks in advance for any assistance. Here is my code: void CTestDlg::SubmitToDbProfile(void) { _RecordsetPtr pRecordSet; HRESULT hr; CString TimeTmp; _variant_t nRecsAffected; _bstr_t bstrQuery("SELECT * FROM COOK.UUT_TEST_PROFILE WHERE test_profile_id IS NULL"); _variant_t vNull; vNull.vt = VT_ERROR; vNull.scode = DISP_E_PARAMNOTFOUND; try { hr = pRecordSet.CreateInstance(_uuidof(Recordset)); if(SUCCEEDED(hr)) { pRecordSet->PutRefActiveConnection(m_pConnection); hr = pRecordSet->Open(_variant_t(bstrQuery), vNull, adOpenDynamic, adLockOptimistic, adCmdText); if(SUCCEEDED(hr)) { COleSafeArray vaFieldlist; vaFieldlist.CreateOneDim(VT_VARIANT, 13); COleSafeArray vaValuelist; vaValuelist.CreateOneDim(VT_VARIANT, 13); long lArrayIndex[1] = {0}; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_facility_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("user_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP1->pUser->ID))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_type_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tech_type_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("region_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tag_pol_magnetic_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tag_speed_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pTagSpeed->ID))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tag_label_type_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("grid_size_id_h"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pGridH->ID))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("grid_size_id_v"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pGridV->ID))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("manufacturer_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tag_characterized_id"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(NULL))); //If characterized, that must be submitted to a table and the index to that table submitted here //vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP1->TagChar))); //lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("noise_inj_id"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(NULL))); //lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("configuration_released"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(NULL))); //lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_name"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(""))); //lArrayIndex[0]++; if(strlen(pTis->pDataPoolPP1->AddtnlNotes) > 0) { vaFieldlist.PutElement(lArrayIndex, &(_variant_t("notes"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP1->AddtnlNotes))); lArrayIndex[0]++; } //markfordelete testing purposes only pTis->TimeStart = CTime::GetCurrentTime(); vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_date_time_start"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(DateTime2DbFormat(&pTis->TimeStart)))); lArrayIndex[0]++; //markfordelete testing purposes only pTis->TimeEnd = CTime::GetCurrentTime(); vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_date_time_end"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(DateTime2DbFormat(&pTis->TimeEnd)))); lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_height_id"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pTestHt->ID))); //lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("ped_cntr_id"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pPedCenter->ID))); //lArrayIndex[0]++; pRecordSet->AddNew(vaFieldlist, vaValuelist); //------------------------------------------------------------- //pRecordSet->Resync causes the following error: //Exc thrown for classes generated by #import Code = 800a0cb3 //Code meaning = Unknown error 0x800A0CB3 //Source = ADODB.Recordset //Description = Current provider does not support refreshing underlying values. //pRecordSet->Resync(adAffectAll, adResyncUnderlyingValues); //------------------------------------------------------------- //Next line: pRecordSet->Fields->GetItem... returns "EMPTY" to _variant_t //_variant_t var = pRecordSet->Fields->GetItem("test_profile_id")->GetValue(); //------------------------------------------------------------- //http://msdn.microsoft.com/en-us/library/ms677536%28VS.85%29.aspx //If you pass the Fieldlist and Values arguments, ADO immediately posts the //new record to the database (no Update call is necessary); //pRecordSet->Update(); //------------------------------------------------------------- //pRecordSet->AbsolutePosition(); //pRecordSet->Requery(adCmdUnknown); //------------------------------------------------------------- //http://support.microsoft.com/kb/233299 //Starting with ADO 2.1, the ADO client cursor engine uses this query to try //to retrieve the new auto-increment value and place that in the appropriate //field in your Recordset. If you add a row to your recordset and that //recordset contains an auto-increment value, ADO will issue the SELECT //@@identity query after the INSERT INTO ... query. //Ssooo... shouldn't need to call "SELECT @@Identity" because ADO does it already?? //Next 3 lines: bstrQuery = "SELECT @@Identity"; conn->Execute;pRecordSet->Fields->GetItem... returns "EMPTY" //bstrQuery = "SELECT @@Identity"; //m_pConnection->Execute(bstrQuery, &nRecsAffected, adCmdUnknown); //_variant_t var = pRecordSet->Fields->GetItem("test_profile_id")->GetValue(); //------------------------------------------------------------- _variant_t vID = pRecordSet->GetCollect("test_profile_id"); pRecordSet->Close(); } } } catch( _com_error &e ) { ::CatchError(e); } catch(...) { ::CatchError(); } } |
#3
| |||
| |||
|
|
Also, I forgot to mention. I am using MS SQL 2005. I am testing with EXPRESS on a local machine and the production server is 2005 (non-EXPRESS). "Jimmie C." wrote: I know this issue has been solved before for some people. I looked all over the net and found some solutions but they do not work for me. I am fairly new to SQL. I am not using stored procedures and due to schedule, do not have time to go down a learning path for that. I am using MFC and ADO. I can add the records (with AddNew) just fine. That works. I am stuck! Thanks in advance for any assistance. Here is my code: void CTestDlg::SubmitToDbProfile(void) { _RecordsetPtr pRecordSet; HRESULT hr; CString TimeTmp; _variant_t nRecsAffected; _bstr_t bstrQuery("SELECT * FROM COOK.UUT_TEST_PROFILE WHERE test_profile_id IS NULL"); _variant_t vNull; vNull.vt = VT_ERROR; vNull.scode = DISP_E_PARAMNOTFOUND; try { hr = pRecordSet.CreateInstance(_uuidof(Recordset)); if(SUCCEEDED(hr)) { pRecordSet->PutRefActiveConnection(m_pConnection); hr = pRecordSet->Open(_variant_t(bstrQuery), vNull, adOpenDynamic, adLockOptimistic, adCmdText); if(SUCCEEDED(hr)) { COleSafeArray vaFieldlist; vaFieldlist.CreateOneDim(VT_VARIANT, 13); COleSafeArray vaValuelist; vaValuelist.CreateOneDim(VT_VARIANT, 13); long lArrayIndex[1] = {0}; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_facility_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("user_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP1->pUser->ID))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_type_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tech_type_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("region_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tag_pol_magnetic_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tag_speed_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pTagSpeed->ID))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tag_label_type_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("grid_size_id_h"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pGridH->ID))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("grid_size_id_v"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pGridV->ID))); lArrayIndex[0]++; vaFieldlist.PutElement(lArrayIndex, &(_variant_t("manufacturer_id"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(1))); lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("tag_characterized_id"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(NULL))); //If characterized, that must be submitted to a table and the index to that table submitted here //vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP1->TagChar))); //lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("noise_inj_id"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(NULL))); //lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("configuration_released"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(NULL))); //lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_name"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(""))); //lArrayIndex[0]++; if(strlen(pTis->pDataPoolPP1->AddtnlNotes) > 0) { vaFieldlist.PutElement(lArrayIndex, &(_variant_t("notes"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP1->AddtnlNotes))); lArrayIndex[0]++; } //markfordelete testing purposes only pTis->TimeStart = CTime::GetCurrentTime(); vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_date_time_start"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(DateTime2DbFormat(&pTis->TimeStart)))); lArrayIndex[0]++; //markfordelete testing purposes only pTis->TimeEnd = CTime::GetCurrentTime(); vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_date_time_end"))); vaValuelist.PutElement(lArrayIndex, &(_variant_t(DateTime2DbFormat(&pTis->TimeEnd)))); lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("test_height_id"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pTestHt->ID))); //lArrayIndex[0]++; //vaFieldlist.PutElement(lArrayIndex, &(_variant_t("ped_cntr_id"))); //vaValuelist.PutElement(lArrayIndex, &(_variant_t(pTis->pDataPoolPP2->pPedCenter->ID))); //lArrayIndex[0]++; pRecordSet->AddNew(vaFieldlist, vaValuelist); //------------------------------------------------------------- //pRecordSet->Resync causes the following error: //Exc thrown for classes generated by #import Code = 800a0cb3 //Code meaning = Unknown error 0x800A0CB3 //Source = ADODB.Recordset //Description = Current provider does not support refreshing underlying values. //pRecordSet->Resync(adAffectAll, adResyncUnderlyingValues); //------------------------------------------------------------- //Next line: pRecordSet->Fields->GetItem... returns "EMPTY" to _variant_t //_variant_t var = pRecordSet->Fields->GetItem("test_profile_id")->GetValue(); //------------------------------------------------------------- //http://msdn.microsoft.com/en-us/library/ms677536%28VS.85%29.aspx //If you pass the Fieldlist and Values arguments, ADO immediately posts the //new record to the database (no Update call is necessary); //pRecordSet->Update(); //------------------------------------------------------------- //pRecordSet->AbsolutePosition(); //pRecordSet->Requery(adCmdUnknown); //------------------------------------------------------------- //http://support.microsoft.com/kb/233299 //Starting with ADO 2.1, the ADO client cursor engine uses this query to try //to retrieve the new auto-increment value and place that in the appropriate //field in your Recordset. If you add a row to your recordset and that //recordset contains an auto-increment value, ADO will issue the SELECT //@@identity query after the INSERT INTO ... query. //Ssooo... shouldn't need to call "SELECT @@Identity" because ADO does it already?? //Next 3 lines: bstrQuery = "SELECT @@Identity"; conn->Execute;pRecordSet->Fields->GetItem... returns "EMPTY" //bstrQuery = "SELECT @@Identity"; //m_pConnection->Execute(bstrQuery, &nRecsAffected, adCmdUnknown); //_variant_t var = pRecordSet->Fields->GetItem("test_profile_id")->GetValue(); //------------------------------------------------------------- _variant_t vID = pRecordSet->GetCollect("test_profile_id"); pRecordSet->Close(); } } } catch( _com_error &e ) { ::CatchError(e); } catch(...) { ::CatchError(); } } |
#4
| |||
| |||
|
|
I know this issue has been solved before for some people. I looked all over the net and found some solutions but they do not work for me. I am fairly new to SQL. I am not using stored procedures and due to schedule, do not have time to go down a learning path for that. |
#5
| |||
| |||
|
|
Jimmie C. (JimmieC (AT) discussions (DOT) microsoft.com) writes: I know this issue has been solved before for some people. I looked all over the net and found some solutions but they do not work for me. I am fairly new to SQL. I am not using stored procedures and due to schedule, do not have time to go down a learning path for that. And so you get stuck with second-guessing a client API instead. Writing a stored procedure to this is a no-brainer: CREATE PROCEDURE insert_a_row @par1 int, @par2 int, @ident int OUTPUT AS SET NOCOUNT ON INSERT tbl(col1, col2) VALUES(@par1, @par2) And the you use CREATE PARAMETER to pass the parameters. If you don't have the time to learn this, you don't have the time for much of all. Least of all to fight with AddNew. By the way, if you are learning things, I cannot really congratulate you to having write ADO code in C++. ADO is not really this year's technology. |
#6
| |||
| |||
|
|
Thanks for your reply. It did not show up in the MSDN web-interface version of the newsgroups (all 3). I logged into my MSDN account to check it. However, it did show up in the newsgroups using the X-news reader. Very strange ?!?!?!? |
|
So, I am replying from X-news reader instead of my MSDN login and web interface. |
|
You mention that ADO is yesterday's news. I knew it was aging. What is the current technology for C++ database interface? Or, has everything just gone to C#? I tend to like C++. |
#7
| |||
| |||
|
|
JC (elvis_is_king (AT) bellsouth (DOT) net) writes: Thanks for your reply. It did not show up in the MSDN web-interface version of the newsgroups (all 3). I logged into my MSDN account to check it. However, it did show up in the newsgroups using the X-news reader. Very strange ?!?!?!? Hm, I never use the web interfaces, so I don't know if there is any hiccup. But maybe I should check it out. So, I am replying from X-news reader instead of my MSDN login and web interface. Xnews? I use it too! Luu rules! You mention that ADO is yesterday's news. I knew it was aging. What is the current technology for C++ database interface? Or, has everything just gone to C#? I tend to like C++. The best option for C++ programmers is probably to use ODBC. It's maybe a little more low-level than ADO, but if you use SQL Server Native Client which comes with SQL Server, you get an API that has full support for new features in SQL Server. SQL Server Native Client is a DLL which includes both an ODBC driver and an OLE DB provider, which makes OLE DB an option too. But OLE DB is quite verbose, and its myriad of interfacing can be daunting. ODBC is likely to be more straightforward. (Myself, I have only used OLE DB, not ODBC.) ADO sits on top of OLE DB, so you can use the SQLNCLI provider with ADO. But ADO does not know of the new data types like xml, so you would lose some functionality. -- Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se Links for SQL Server Books Online: SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx |
#8
| |||||
| |||||
|
|
As far as the web interface not being updated, it was either the cookies or the fact that our IT department caches a lot pages locally. |
|
You know, when I started with this little project, I was going to use ODBC but I was told by some colleagues that it is depricated and that I should use ADO as that is the next thing in the MSoft path. |
|
Here are a few problems I have found with learning ADO: 1)GetCollect is used in a lot of examples but not well documented. |
|
2)When there is a problem with the results, it does not give a clear indication of what went wrong. The inner workings are not explained very well. |
|
3)The biggest issue is that most of the examples are in VB!!!!! |
#9
| |||
| |||
|
|
Here are a few problems I have found with learning ADO: 1)GetCollect is used in a lot of examples but not well documented. |
|
Have I ever seen that one? |
|
JC (elvis****_is_***king**at**bellsouth***dot***net) writes: As far as the web interface not being updated, it was either the cookies or the fact that our IT department caches a lot pages locally. Ah, that's good to know. Then I should not research any further! You know, when I started with this little project, I was going to use ODBC but I was told by some colleagues that it is depricated and that I should use ADO as that is the next thing in the MSoft path. When did you start the project? In 1999? :-) It's not always easy to follow Microsoft as new things come out. ADO certainly was king for a while, and ODBC was not really so hot. But OLE DB never took off as Microsoft had intended. There are still many data sources for which there is not an OLE DB provider, but there is an ODBC driver. Here are a few problems I have found with learning ADO: 1)GetCollect is used in a lot of examples but not well documented. Have I ever seen that one? 2)When there is a problem with the results, it does not give a clear indication of what went wrong. The inner workings are not explained very well. Yes, error diagnosis with ADO is not that good. Most of all, you don't get all information SQL Server provides. 3)The biggest issue is that most of the examples are in VB!!!!! Yup. If you are in Visual Basic, admittedly ADO is your best choice. I don't think that you can all the ODBC API directly from VB. But even if can, ADO is likely to be more conventient. From VB, that is. -- Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se Links for SQL Server Books Online: SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx |
#10
| |||
| |||
|
|
If you look at my most recent post about writing PDF files to the Db, you will see an example where I am using GetCollect(). |
|
I guess I will have to learn VB or C# or become extinct. Wait wasn't C++ supposed to be more powerful and surpass VB?!?! Oh well. |
|
On a side-note, I see that you were able to actually reply to my last post. I tried about 7 times to get it to post. |
![]() |
| Thread Tools | |
| Display Modes | |
| |