Re: Access Databases, MFC and VS2005
saywhat ??:
Okay. I'm going to try and ask this as succinctly as possible.
When using an Access database in my apps, I've had to derive a recordset for
each table. Before you laugh, consider that there are all of 3 tables in
each, and each of those has less than 20 columns.
My preferred way of using this techniques is to use the wizard to create a
class derived from CRecordset and indicate the desired table in the dialogs.
It works well but it's a lot of code, particularly if I want to do anything
relational between tables.
I have read and re re re read the MS docs, but I cannot seem to figure out
just how to use SQL statements for queries and to return recordsets for an
Access DB. This is especially true of paramaterized queries. I know that
if I selct ALL the tables in the wizard and create one big recordset class
it seems messy, to me.
Do I use the Open function and specify my sql as a string? Does this work
with creating the recordset in the wizrd? Where can I find some more
resources?
Thanks
b
Refer to the following code snippet that executes direct SQL and get result.
CDatabase db;
db.OpenEx("Driver={Microsoft Access Driver
(*.mdb)};DBQ=C:\\temp\\db1.mdb;");
CRecordset rs(&db);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select * From table1");
int nfieldcount=rs.GetODBCFieldCount();
CString msg;
msg.Format("There are %d fields in table1",nfieldcount);
AfxMessageBox(msg);
//field 0 is int field,name "intfield",retrieve by field index
CDBVariant dbvar;
rs.GetFieldValue((short)0,dbvar);
//field 1 is string field,name "strfield",retrieve by field name
CString dbstrvar;
rs.GetFieldValue("strfield",dbstrvar);
msg.Format("field 0 value:%d,field name strfield
value=%s",dbvar.m_iVal,dbstrvar);
AfxMessageBox(msg);
rs.Close();
db.Close();