Chitika

Thursday, January 26, 2012

MFC Database

Connection:

CDatabase dbase;
dbase.OpenEx("DSN=ecx;UID=sa;PWD=wavetec;");
...
dbase.Close();

Retrieve:

CRecordset rs(&dbase);
rs.Open(CRecordset::dynaset, _T("select id,c1, c2, c3 from tbl")); // preserve same order
                                                                                                       //while reading c1, c2,c3

     CDBVariant varValue;

        while( !rs.IsEOF( ) )
        {
           rec.GetFieldValue(_T("id"), varValue, SQL_C_SLONG);
          id = varValue.m_lVal;

          rec.GetFieldValue(_T("c1"), varValue, SQL_C_CHAR);
          val = varValue.m_pstringA->GetString();
          ...
          
           rs.MoveNext( );
        }

        rs.Close();

Update/Delete:

        CString strQl = "update tble set ts=getdate() where c1='";
        strQl += c1
        strQl += "'";

        dbase.ExecuteSQL(strQl);

No comments:

Post a Comment