}我的代码是放在MFC一个按钮Click事件里面的:
记住在处理事件的cpp文件中导入头文件:#include “DBOperation.h”
[cpp]
CDBOperation dbOper;
bool bConn = dbOper.ConnToDB(“Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=boss”, “用户名”, “密码”);
if (false == bConn)
{
MessageBox((LPCTSTR)“连接数据库出现错误\0”,0,0);
return;
}
//查询
_RecordsetPtr pRst;
char sql[255] = {0};
strcpy(sql, “ select * from boss_test_table2 where rownum = 1 ”);
pRst = dbOper.ExecuteWithResSQL(sql);
if (NULL == pRst)
{
MessageBox(_T(“查询数据出现错误!\0”),0,0);
return;
}
if (pRst->adoEOF)
{
pRst->Close();
MessageBox((LPCTSTR)“There is no records in this table\0”,0,0);
return;
}
_variant_t vSno, vName;
while (!pRst->adoEOF)
{
//pRst->MoveFirst(); //记录集指针移动到查询结果集的前面
vSno = pRst->GetCollect(_variant_t(“U_NUMBER”));
vName = pRst->GetCollect(_variant_t(“USERS_NAME”));
MessageBox((LPCTSTR)(_bstr_t)vSno,0,0);
pRst->MoveNext();
}
strcpy(sql, “insert into boss_test_table2 (u_number, users_name, users_phone, status, customno_id) values ('0001', 'C+TTT+', '13999000000', 2, 'BPPPPPPPPPP')”);
pRst = dbOper.ExecuteWithResSQL(sql);
if (NULL != pRst)
{
AfxMessageBox(_T(“插入数据成功\n”));
}
//执行删除语句
sprintf(sql, “delete boss_test_table2 where u_number = '%s‘”, “009”);
pRst = dbOper.ExecuteWithResSQL(sql);
if (NULL != pRst)
{
MessageBox(_T(“删除数据成功\0”),0,0);
}
//执行更新语句
sprintf(sql, “update boss_test_table2 set users_name = '%s' ”, “C++(www.cppentry.com)反人类、MFC反社会”);
pRst = dbOper.ExecuteWithResSQL(sql);
if (NULL != pRst)
{
MessageBox(_T(“更新数据成功\0”),0,0);
}
CDBOperation dbOper;
bool bConn = dbOper.ConnToDB(“Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=boss”, “用户名”, “密码”);
if (false == bConn)
{
MessageBox((LPCTSTR)“连接数据库出现错误\0”,0,0);
return;
}
//查询
_RecordsetPtr pRst;
char sql[255] = {0};
strcpy(sql, “ select * from boss_test_table2 where rownum = 1 ”);
pRst = dbOper.ExecuteWithResSQL(sql);
if (NULL == pRst)
{
MessageBox(_T(“查询数据出现错误!\0”),0,0);
return;
}
if (pRst->adoEOF)
{
pRst->Close();
MessageBox((LPCTSTR)“There is no records in this table\0”,0,0);
return;
}
_variant_t vSno, vName;
while (!pRst->adoEOF)
{
//pRst->MoveFirst(); //记录集指针移动到查询结果集的前面
vSno = pRst->GetCollect(_variant_t(“U_NUMBER”));
vName = pRst->GetCollect(_variant_t(“USERS_NAME”));
MessageBox((LPCTSTR)(_bstr_t)vSno,0,0);
pRst->MoveNext();
}
strcpy(sql, “insert into boss_test_table2 (u_number, users_name, users_phone, status, customno_id) values ('0001', 'C+TTT+', '13999000000', 2, 'BPPPPPPPPPP')”);
pRst = dbOper.ExecuteWithResSQL(sql);
if (NULL != pRst)
{
AfxMessageBox(_T(“插入数据成功\n”));
}
//执行删除语句
sprintf(sql, “delete boss_test_table2 where u_number = '%s’”, “009”);
pRst = dbOper.ExecuteWithResSQL(sql);
if (NULL != pRst)
{
MessageBox(_T(“删除数据成功\0”),0,0);
}
//执行更新语句
sprintf(sql, “update boss_test_table2 set users_name = '%s' ”, “C++(www.cppentry.com)反人类、MFC反社会”);
pRst = dbOper.ExecuteWithResSQL(sql);
if (NULL != pRst)
{
MessageBox(_T(“更新数据成功\0”),0,0);
}