|
-
October 17th, 2007, 03:17 PM
#4
Re: ADO and UpdateBatch with SQL Server
Boy oh Boy -
It's been some time since we talked about ADO Disconnected Recordset. I have returned to this issue and still have some problems.
In the code below only the last value is updated (222). I'm not sure if the MoveNext and Update commands are working with the disconnected recordset.
Also I could not get the insert to work using the AddNew() / Update()
Here is some code to chew on.
Comments are welcome.
Code:
// Define ADO connection pointers
_ConnectionPtr pConnection = NULL;
_RecordsetPtr pRecordset = NULL;
try
{
// When we open the application we will open the ADO connection
pConnection.CreateInstance(__uuidof(Connection));
// Replace Data Source value with your server name.
bstr_t bstrConnect("Provider='sqloledb';Data Source='BVLSQLTEST1';"
"Initial Catalog='AlphaNumericData';"
"User Id=cmacgowan;Password=cmacgowan");
// Open the ado connection
pConnection->Open(bstrConnect,"","",adConnectUnspecified);
// Create an instance of the database
pRecordset.CreateInstance(__uuidof(Recordset));
// Select the correct sql string. Note that we are creating an
// empty string by doing a select on the primary key. We are only
// doing inserts and we do not want to bring data back from the
// server
pRecordset->CursorLocation = adUseClient;
pRecordset->PutRefActiveConnection(pConnection);
pRecordset->LockType = adLockBatchOptimistic;
csSQL = "SELECT * FROM dbo.MacgowanDCR1H";
// pRecordset->Open(csSQL.AllocSysString(), vNull, adOpenStatic, adLockOptimistic, -1);
pRecordset->Open(csSQL.AllocSysString(),
pConnection.GetInterfacePtr(),
adOpenForwardOnly,
adLockOptimistic,
-1);
// spRS->Open(OLESTR("select * from Table1"),
// spConn.GetInterfacePtr(),
// adOpenForwardOnly, adLockBatchOptimistic, -1);
// Move to the top oc the recordset
// pRecordset->MoveFirst();
// Disassociate the connection from the recordset.
pRecordset->PutRefActiveConnection(NULL);
// Change some data
sval=((long) 111);
pRecordset->Fields->GetItem(L"DIcastId")->PutValue(sval);
pRecordset->Update();
pRecordset->MoveNext();
// Change some data
sval=((long) 222);
pRecordset->Fields->GetItem(L"DIcastId")->PutValue(sval);
/*
// Scroll through all the records and update the DIcastId
while (pRecordset->GetadoEOF() == VARIANT_FALSE)
{
// Change some data
sval=((long) 888);
pRecordset->Fields->GetItem(L"DIcastId")->PutValue(sval);
// Update
//pRecordset->Update();
pRecordset->MoveNext();
}
*/
// Now we will add some new recorrds
int nCount = 0;
int i = 0;
for (i=0; i < nCount; i++)
{
// insert a new row
pRecordset->AddNew();
// Get the DIcastId data and set the recordset
// strTemp = pMxTextParse->GetParseData("DIcastId");
// nTempLong = atol(strTemp.c_str());
sval=((long) i);
pRecordset->Fields->GetItem(L"DIcastId")->PutValue(sval);
// Get the DIcastId data and set the recordset
strTemp = (string)"ABCD";
sval.SetString(strTemp.c_str());
pRecordset->Fields->GetItem(L"StationID")->PutValue(sval);
// Get the StationLatitude data and set the recordset
strTemp = (string)"232.645";
nTempDouble = atof(strTemp.c_str());
sval=((double) nTempDouble);
pRecordset->Fields->GetItem(L"Latitude")->PutValue(sval);
// Get the StationLatitude data and set the recordset
strTemp = (string)"123.59";
nTempDouble = atof(strTemp.c_str());
sval=((double) nTempDouble);
pRecordset->Fields->GetItem(L"Longitude")->PutValue(sval);
// COleDateTime is a wrapper for VARIANT's DATE type. COleVariant is
// a wrapper for VARIANTs themselves. If you need to create a
// variant, you can say:
COleDateTime oledtCurrentDate2 = COleDateTime::GetCurrentTime();
// Convert the OleDateTime to the varient
COleVariant vCurrentDateTime2(oledtCurrentDate2);
// Set the ModelRunDateTime and ValidDateTime
pRecordset->Fields->GetItem(L"ModelRunDateTime")->PutValue(vCurrentDateTime2);
pRecordset->Fields->GetItem(L"ValidDateTime")->PutValue(vCurrentDateTime2);
// Get the Temperature data and set the recordset
strTemp = (string)"54";
nTempInt = atoi(strTemp.c_str());
sval=((short) nTempInt);
pRecordset->Fields->GetItem(L"Temperature")->PutValue(sval);
// Get the WindDirection data and set the recordset
strTemp = (string)"180";
nTempInt = atoi(strTemp.c_str());
sval=((short) nTempInt);
pRecordset->Fields->GetItem(L"WDIR")->PutValue(sval);
// Get the WindSpeed data and set the recordset
strTemp = (string)"900";
nTempInt = atoi(strTemp.c_str());
sval=((short) nTempInt);
pRecordset->Fields->GetItem(L"WSPD")->PutValue(sval);
// Update
pRecordset->Update();
}
// Re-connect.
pRecordset->PutRefActiveConnection(pConnection);
// Send updates.
pRecordset->UpdateBatch(adAffectAll);
pRecordset->Close();
pConnection->Close();
// End of test
}
catch(_com_error *e)
{
CString Error = e->ErrorMessage();
AfxMessageBox(e->ErrorMessage());
pView->WriteLog("Error processing TestDatabase().");
}
catch(...)
{
csMessage = "Undefined exception handled. Error message details \n\n";
hResult = GetAdoErrorMessage(pConnection,
&csErrorMessage);
csMessage += csErrorMessage;
csMessage += "\nmethod: CTestMeteorlogixApp::TestDatabaseUpdateBatch3()";
AfxMessageBox(csMessage);
}
pView->WriteLog("End TestDatabaseUpdateBatch3.");
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|