DataBase and ATL
-
I'm using CDataSource, CSession etc to work with some database. This database is having, for example, one table with two columns. One is a self-incrementing counter (index) and other is a value. My task is to write a function like this:
long Add(long val) {
// bla-bla-bla
return index_of_val_added;
}I'm doing it this way:
long Add(long val) {
CCommand< CAccessor< table > > t;
t.Open(session, "insert into table(val) values(-1);", NULL, NULL, DBGUID_DEFAULT, FALSE);
t.Close();
t.Open(session, "select * from table where val=-1;");
int id = t.m_id; // got it
t.Close();
CString s;
s.Format("update table set val=%d where id=%d", val, id);
t.Open(session, s, NULL, NULL, DBGUID_DEFAULT, FALSE);
t.Close();
return id;
}I know, that it is ugly... Could anybody show me the correct way to do it? With the best regards, Vitaly.