Click here to Skip to main content
15,911,711 members
Home / Discussions / Database
   

Database

 
QuestionCase sensitive queries? Pin
LukeV23-Mar-03 6:33
LukeV23-Mar-03 6:33 
AnswerRe: Case sensitive queries? Pin
Richard Deeming24-Mar-03 7:34
mveRichard Deeming24-Mar-03 7:34 
GeneralADO + SQL + FOXPRO Question/problem Pin
iluha22-Mar-03 11:11
iluha22-Mar-03 11:11 
GeneralRe: ADO + SQL + FOXPRO Question/problem Pin
Marek Konieczny27-Mar-03 1:43
Marek Konieczny27-Mar-03 1:43 
QuestionSQL interactive training? Pin
Duncan Edwards Jones21-Mar-03 0:05
professionalDuncan Edwards Jones21-Mar-03 0:05 
GeneralSQL Performance UserConnections Pin
Peter Kiss20-Mar-03 12:44
Peter Kiss20-Mar-03 12:44 
GeneralODBC and CListCtrl Pin
MemLeak20-Mar-03 10:17
MemLeak20-Mar-03 10:17 
GeneralAdding New records to Access DB Pin
Aaron Schaefer19-Mar-03 9:31
Aaron Schaefer19-Mar-03 9:31 
Hi All,

I'm trying to imlpement a helper .dll for managing some data on an Access DB using ADO. I found some sample code, that is working (sort of), but not too reliably. What I'm trying to figure out how to do is to populate some tables with new records, say 500+ at a time.

Basically, I open a recordset with the name of the table, then for each row, call AddNew, and set the values of the fields. After I'm done with all the rows, I call the Update method of the recordSet. Thing is, this works OK only if I do it kind of slow. I've got a Sleep(100) between each row, then I get all the data into my database. If I comment this out, I get nuthin'.

What's the deal? How would you do something like this:

struct Data
{
std::string Name;
double dWeight;
}

const Data arrData[] =
{
"Bob", 175.00,
"Tina", 130.00,
// lots more rows
};

Move all of this into an Access DB all at once.

Here's my actual code:

// To append a single row to the DB
HRESULT CHTDMBridge::AppendRow(/*const _bstr_t& bstrTable, */ValueMap& values)
{
// Must be connected
HRESULT hr;
if(m_pConnection == NULL) return E_FAIL;
if(m_pRecordSet == NULL) return E_FAIL;

// Going too fast for ACCESS??
// Sleep(100);

try
{
// Create a new record
hr = m_pRecordSet->AddNew();
if(FAILED(hr)) printf("\nOops!");

// For each field
ADO::FieldPtr pField;
_variant_t vField;
_variant_t vValue;
ValueMapIterator it;
for(it = values.begin(); it != values.end(); it++)
{
try
{
vField = (*it).first;
vValue = (*it).second;

// Set the value of the indicated fiels (it better be there)
m_pRecordSet->Fields->GetItem(vField)->Value = vValue;

}
catch(...)
{
printf("\nError updating fields in new record");
PrintProviderErrors(m_pConnection);
}
}

}
catch(_com_error e)
{
printf("\nError: %s, Desc: %s", e.ErrorMessage(), e.Description());
PrintProviderErrors(m_pConnection);
}
catch(...)
{
}

return S_OK;
}


// To append a lot of rows:
template<class _recordtype="">
void AppendRows(const char* szTableName, std::vector<_RecordType>& vct)
{
// Open the table
OpenTable(szTableName);

// For each record
std::vector<_RecordType>::iterator it;
ValueMap values;
_RecordType record;
for(it = vct.begin(); it != vct.end(); it++)
{
values = *it;
AppendRow(values);
}

// Update the database
HRESULT hr = m_pRecordSet->Update();
if(FAILED(hr)) printf("\nOops!");
m_pRecordSet->Close();

}


// To open the recordset to a given table
void OpenTable(const std::string& szTableName)
{
try
{
// Open the table
_variant_t vConnection = m_pConnection.GetInterfacePtr();
_bstr_t bstrTable = szTableName.c_str();
_variant_t vSource = bstrTable;
m_pRecordSet->Open(vSource, vConnection, ADO::adOpenForwardOnly, ADO::adLockPessimistic, ADO::adCmdTableDirect);
}
catch(_com_error e)
{
printf("\nCHTDMBridge::OpenTable(%s), Error: %s, Desc: %s", szTableName.c_str(), e.ErrorMessage(), e.Description());
PrintProviderErrors(m_pConnection);
}
catch(...)
{
}
}

Any help is appreciated.

Aaron
GeneralProblem using UPDATE Pin
Steve McLenithan19-Mar-03 2:51
Steve McLenithan19-Mar-03 2:51 
GeneralRe: Problem using UPDATE Pin
Philip Patrick19-Mar-03 10:03
professionalPhilip Patrick19-Mar-03 10:03 
GeneralRe: Problem using UPDATE Pin
Steve McLenithan20-Mar-03 10:40
Steve McLenithan20-Mar-03 10:40 
GeneralRe: Problem using UPDATE Pin
Philip Patrick20-Mar-03 10:56
professionalPhilip Patrick20-Mar-03 10:56 
GeneralRe: Problem using UPDATE Pin
Rein Hillmann21-Mar-03 10:08
Rein Hillmann21-Mar-03 10:08 
GeneralRe: Problem using UPDATE Pin
Steve McLenithan21-Mar-03 12:27
Steve McLenithan21-Mar-03 12:27 
GeneralRe: Problem using UPDATE Pin
Rein Hillmann21-Mar-03 13:20
Rein Hillmann21-Mar-03 13:20 
GeneralRe: Problem using UPDATE Pin
Steve McLenithan21-Mar-03 13:53
Steve McLenithan21-Mar-03 13:53 
GeneralRe: Problem using UPDATE [ prob Narrowed down!!] Pin
Steve McLenithan21-Mar-03 20:16
Steve McLenithan21-Mar-03 20:16 
GeneralRe: Problem using UPDATE Pin
SimonS22-Mar-03 1:41
SimonS22-Mar-03 1:41 
GeneralRe: Problem using UPDATE Pin
Richard Deeming24-Mar-03 7:44
mveRichard Deeming24-Mar-03 7:44 
GeneralRe: Problem using UPDATE Pin
Steve McLenithan24-Mar-03 7:46
Steve McLenithan24-Mar-03 7:46 
GeneralAny Access experts here Pin
Michael P Butler18-Mar-03 23:13
Michael P Butler18-Mar-03 23:13 
GeneralRe: Any Access experts here Pin
Jeremy Oldham19-Mar-03 1:39
Jeremy Oldham19-Mar-03 1:39 
GeneralRe: Any Access experts here Pin
Michael P Butler19-Mar-03 3:25
Michael P Butler19-Mar-03 3:25 
GeneralDBConcurrencyException: Delete a entry in master -detail table Pin
DionChen18-Mar-03 10:16
DionChen18-Mar-03 10:16 
QuestionAlternatives to Access??? Pin
LukeV18-Mar-03 3:05
LukeV18-Mar-03 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.