对access进行循环查询操作,应该怎样优化比较好?
比如我的代码如下所示:
for (int i=0; i<szSecondRecordTypeArray.GetSize(); i++)
{
CString szSecondRecordTypeCode = szSecondRecordTypeArray.GetAt(i);
CString szMXRefSysSQL = "Select * from errorinfo where key_field = '" + szSecondRecordTypeCode + "'order by err_code";
if(pset->Open(szMXRefSysSQL.GetBuffer(0)))
{
//具体操作
}
}
Select * from errorinfo order by key_field, err_code
#include "stdafx.h"
#include "XproerDataDef.h"
#include "OleDb/OleDbCommand.h"
#include "OleDb/OleDbConnection.h"
#include "OleDb/OleDbDataReader.h"
#include "OleDb/DataTable.h"
#include "OleDb/DataRow.h"
#include "OleDb/DataValue.h"
using namespace Xproer::OleDb;
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
OleDbConnection con;
//设置Access数据库,兼容Access 97,Access 2000,Access 2003,Access 2007,Access 2010
con.SetAccessConStr(L"F:\\vc9\\adoDemo\\Debug\\db.mdb");
con.Open();
OleDbCommand cmd(&con);
//设置SQL命令
cmd.SetCommandText(L"delete from xdb_news where id=1");
cmd.ExecuteNonQuery();
con.Close();//关闭数据库连接
::CoUninitialize();
system("pause");
return 0;
}
#include "stdafx.h"
#include "XproerDataDef.h"
#include "OleDb/OleDbCommand.h"
#include "OleDb/OleDbConnection.h"
#include "OleDb/OleDbDataReader.h"
#include "OleDb/DataTable.h"
#include "OleDb/DataRow.h"
#include "OleDb/DataValue.h"
using namespace Xproer::OleDb;
//获取DataTable的示例
void ADODataTable(OleDbCommand& cmd)
{
int count = 0;
cmd.ExecuteScalar(L"select count(*) from xdb_news",count);
auto_ptr<DataTable> table;
table.reset(cmd.ExecuteDataTable(L"select top 10 IsTop from xdb_news"));
size_t rowCount = table->GetRowsCount();
for (int i = 0 ; i < rowCount ; ++i)
{
DataRow* row = table->GetRow(i);
DataValue* val = row->GetCel(0);
printf("时间:%d \r\n", val->ToBoolean() );
}
}
//使用OleDbDataReader快速读取数据的示例
void ADORead(OleDbCommand& cmd)
{
OleDbDataReader* r = cmd.ExecuteReader(L"select ProductPrice from xdb_news");
USES_CONVERSION;
wstring title;
while(r->Read())
{
//r->GetString(0,title);
//printf("标题:%s \r\n",W2A(title.c_str()));
printf("ID:%f \r\n", r->GetFloat(0) );
}
r->Close();
}
//通过变量向数据库安全添加数据的示例
void ADOAdd(OleDbCommand& cmd)
{
cmd.SetCommandText(L"insert into xdb_about(VDouble) values(@VDouble)");
cmd.AddDoubleParam(L"@VDouble",125022.3180);
cmd.ExecuteNonQuery();
}
//高性能向数据库插入数据的示例
void ADOParam(OleDbCommand& cmd)
{
cmd.SetCommandText(L"delete from xdb_news where ID=@ID;");
cmd.AddInt32Param(L"@ID",886);
cmd.SetPrepared(true);
_variant_t index = (short)0;
_ParameterPtr param = cmd.GetCommand()->Parameters->GetItem(index);
param->Value = 887;
cmd.ExecuteNonQuery();
param->Value = 888;
cmd.ExecuteNonQuery();
param->Value = 889;
cmd.ExecuteNonQuery();
param->Value = 890;
cmd.ExecuteNonQuery();
param->Value = 891;
cmd.ExecuteNonQuery();
}
//向数据库添加实数的示例
void NumericScaleX(OleDbCommand& cmd)
{
OleDbDataReader* r = cmd.ExecuteReader(L"select ProductPrice from xdb_news");
_RecordsetPtr& rs = r->GetRecordsetPtr();
rs->MoveFirst();
_variant_t vi = (short)0;
FieldPtr fd = rs->Fields->GetItem(vi);
unsigned char numeri = fd->GetNumericScale();
unsigned char precision = fd->GetPrecision();
printf("GetNumericScale:%d\r\n", fd->GetNumericScale() );
printf("GetPrecision:%d\r\n", fd->GetPrecision() );
printf("Value:%f\r\n", (float)fd->Value );
r->Close();
}
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
OleDbConnection con;
con.SetAccessConStr(L"F:\\vc9\\adoDemo\\Debug\\db.mdb");
con.Open();
OleDbCommand cmd(&con);
ADORead(cmd);
con.Close();
::CoUninitialize();
//printf("总数:%d",count);
system("pause");
return 0;
}
无论是VC数据库开发新手,还是一个拥有多年VC数据库开发经验的人都能够在10分钟内掌握Xproer.OleDb类库的使用。Xproer.OleDb类库为开发人员带来的不仅是开发工作效率的提升,更是为解决更复杂的业务需求提供了最佳的解决方案。
以下示例演示如何从数据表中删除一条数据
#include "stdafx.h"
#include "XproerDataDef.h"
#include "OleDb/OleDbCommand.h"
#include "OleDb/OleDbConnection.h"
#include "OleDb/OleDbDataReader.h"
#include "OleDb/DataTable.h"
#include "OleDb/DataRow.h"
#include "OleDb/DataValue.h"
using namespace Xproer::OleDb;
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
OleDbConnection con;
//设置Access数据库,兼容Access 97,Access 2000,Access 2003,Access 2007,Access 2010
con.SetAccessConStr(L"F:\\vc9\\adoDemo\\Debug\\db.mdb");
con.Open();
OleDbCommand cmd(&con);
//设置SQL命令
cmd.SetCommandText(L"delete from xdb_news where id=1");
cmd.ExecuteNonQuery();
con.Close();//关闭数据库连接
::CoUninitialize();
system("pause");
return 0;
}
#include "stdafx.h"
#include "XproerDataDef.h"
#include "OleDb/OleDbCommand.h"
#include "OleDb/OleDbConnection.h"
#include "OleDb/OleDbDataReader.h"
#include "OleDb/DataTable.h"
#include "OleDb/DataRow.h"
#include "OleDb/DataValue.h"
using namespace Xproer::OleDb;
//获取DataTable的示例
void ADODataTable(OleDbCommand& cmd)
{
int count = 0;
cmd.ExecuteScalar(L"select count(*) from xdb_news",count);
auto_ptr<DataTable> table;
table.reset(cmd.ExecuteDataTable(L"select top 10 IsTop from xdb_news"));
size_t rowCount = table->GetRowsCount();
for (int i = 0 ; i < rowCount ; ++i)
{
DataRow* row = table->GetRow(i);
DataValue* val = row->GetCel(0);
printf("时间:%d \r\n", val->ToBoolean() );
}
}
//使用OleDbDataReader快速读取数据的示例
void ADORead(OleDbCommand& cmd)
{
OleDbDataReader* r = cmd.ExecuteReader(L"select ProductPrice from xdb_news");
USES_CONVERSION;
wstring title;
while(r->Read())
{
//r->GetString(0,title);
//printf("标题:%s \r\n",W2A(title.c_str()));
printf("ID:%f \r\n", r->GetFloat(0) );
}
r->Close();
}
//通过变量向数据库安全添加数据的示例
void ADOAdd(OleDbCommand& cmd)
{
cmd.SetCommandText(L"insert into xdb_about(VDouble) values(@VDouble)");
cmd.AddDoubleParam(L"@VDouble",125022.3180);
cmd.ExecuteNonQuery();
}
//高性能向数据库插入数据的示例
void ADOParam(OleDbCommand& cmd)
{
cmd.SetCommandText(L"delete from xdb_news where ID=@ID;");
cmd.AddInt32Param(L"@ID",886);
cmd.SetPrepared(true);
_variant_t index = (short)0;
_ParameterPtr param = cmd.GetCommand()->Parameters->GetItem(index);
param->Value = 887;
cmd.ExecuteNonQuery();
param->Value = 888;
cmd.ExecuteNonQuery();
param->Value = 889;
cmd.ExecuteNonQuery();
param->Value = 890;
cmd.ExecuteNonQuery();
param->Value = 891;
cmd.ExecuteNonQuery();
}
//向数据库添加实数的示例
void NumericScaleX(OleDbCommand& cmd)
{
OleDbDataReader* r = cmd.ExecuteReader(L"select ProductPrice from xdb_news");
_RecordsetPtr& rs = r->GetRecordsetPtr();
rs->MoveFirst();
_variant_t vi = (short)0;
FieldPtr fd = rs->Fields->GetItem(vi);
unsigned char numeri = fd->GetNumericScale();
unsigned char precision = fd->GetPrecision();
printf("GetNumericScale:%d\r\n", fd->GetNumericScale() );
printf("GetPrecision:%d\r\n", fd->GetPrecision() );
printf("Value:%f\r\n", (float)fd->Value );
r->Close();
}
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
OleDbConnection con;
con.SetAccessConStr(L"F:\\vc9\\adoDemo\\Debug\\db.mdb");
con.Open();
OleDbCommand cmd(&con);
ADORead(cmd);
con.Close();
::CoUninitialize();
//printf("总数:%d",count);
system("pause");
return 0;
}