一、使用TADOCommand,比直接使用TADOQuery执行insert语句快1倍,1k条记录插入大约只需用1s
auto_ptr
cmd->Connection = adoQry->Connection;
cmd->Connection->BeginTrans();//cmd->Prepared配合使用,速度才能提高
cmd->CommandText = "insert into BAS_Runview(TrainNo,"
"station,sno,[day],ArrivalTime,StartTime,RunTime,RunDist,p1,p2,p3,p4)"
" values(:no,:station,:sno,:day,:arrival,:start,:time,:dist,:p1,:p2,:p3,:p4)";
cmd->Prepared = true;//很重要
for(int j=0; j
QueryBTrainViewResult& runViewRet = *recvRets[j];
int size = runViewRet.items_size();
for(int i=0; i
const BTrainView& runView = runViewRet.items(i);
Variant vDay;
if(runView.has_day())
{
vDay = runView.day();
}
else
{
vDay = Null();
}
Variant vArrival;
if(runView.has_timearrive())
{
vArrival = TimeZero2BJ(runView.timearrive());
}
else
{
vArrival = Null();
}
Variant vStart;
if(runView.has_timestart())
{
vStart = TimeZero2BJ( runView.timestart() );
}
else
{
vStart = Null();
}
Variant vTime;
if(runView.has_runperiod())
{
vTime = runView.runperiod().c_str();
}
else
{
vTime = Null();
}
Variant vDist;
if(runView.has_rundist())
{
vDist = runView.rundist();
}
else
{
vDist = Null();
}
Variant vP1;
if(runView.has_p1())
{
vP1 = runView.p1().c_str();
}
else
{
vP1 = Null();
}
Variant vP2;
if(runView.has_p2())
{
vP2 = runView.p2().c_str();
}
else
{
vP2 = Null();
}
Variant vP3;
if(runView.has_p3())
{
vP3 = runView.p3().c_str();
}
else
{
vP3 = Null();
}
Variant vP4;
if(runView.has_p4())
{
vP4 = runView.p4().c_str();
}
else
{
vP4 = Null();
}
cmd->Parameters->ParamValues["no"]=runView.trainnum().c_str();
cmd->Parameters->ParamValues["station"]=runView.station().c_str();
cmd->Parameters->ParamValues["sno"]=runView.sno();
cmd->Parameters->ParamValues["day"] = vDay;
cmd->Parameters->ParamValues["arrival"] = vArrival;
cmd->Parameters->ParamValues["start"] = vStart;
cmd->Parameters->ParamValues["time"] = vTime;
cmd->Parameters->ParamValues["dist"] = vDist;
cmd->Parameters->ParamValues["p1"] = vP1;
cmd->Parameters->ParamValues["p2"] = vP2;
cmd->Parameters->ParamValues["p3"] = vP3;
cmd->Parameters->ParamValues["p4"] = vP4;
cmd->Execute();
}
}
cmd->Connection->CommitTrans();
二、使用文本文件导入,此方法适用于几十甚至上百万条记录(70W条记录,大约10S完成)
auto_ptr
//第一行表示字段名称,用逗号隔开
AnsiString header = "TrainNo,station,sno,day,ArrivalTime,StartTime,RunTime,RunDist,p1,p2,p3,p4\r\n";
ms->WriteBuffer(header.c_str(),header.Length());
for(int j=0; j
QueryBTrainViewResult& runViewRet = *recvRets[j];
int size = runViewRet.items_size();
for(int i=0; i
const BTrainView& runView = runViewRet.items(i);
AnsiString row = AnsiString(runView.trainnum().c_str()) + "," +
AnsiString(runView.station().c_str()) + "," +
IntToStr((int)runView.sno()) + ",";
if(runView.has_day())
{
row += IntToStr((int)runView.day()) + ",";
}
else
{
row += "null,";
}
if(runView.has_timearrive())
{
row += TimeZero2BJ(runView.timearrive()).DateTimeString() + ",";
}
else
{
row += "null,";
}
if(runView.has_timestart())
{
row += TimeZero2BJ(runView.timestart()).DateTimeString() + ",";
}
else
{
row += "null,";
}
if(runView.has_runperiod())
{
row += runView.runperiod().c_str();
row += ",";
}
else
{
row += "null,";
}
if(runView.has_rundist())
{
row += IntToStr((int)runView.rundist()) + ",";
}
else
{
row += "null,";
}
if(runView.has_p1())
{
row += runView.p1().c_str();
row += ",";
}
else
{
row += "null,";
}
if(runView.has_p2())
{
row += runView.p2().c_str();
row += ",";
}
else
{
row += "null,";
}
if(runView.has_p3())
{
row += runView.p3().c_str();
row += ",";
}
else
{
row += ",";
}
if(runView.has_p4())
{
row += runView.p4().c_str();
}
row += "\r\n";
ms->WriteBuffer(row.c_str(),row.Length());
}
}
String appPath = ExtractFilePath(Application->ExeName);
String txtFileName = "runview.txt";
String txtFilePath = appPath + txtFileName;
if(FileExists(txtFilePath))
{
DeleteFile(txtFilePath);
}
ms->SaveToFile(txtFilePath);//创建临时文本文件
adoQry->ParamCheck = false;
//如果插入到新表中(如果表已经存在需要先删除表),使用如下sql语句:SELECT * INTO [BAS_Runview] FROM...
adoQry->SQL->Text = "INSERT INTO [BAS_Runview] select * FROM "
"[Text;FMT=Delimited;HDR=Yes;DATABASE=" + appPath + "].[" + txtFileName +"];";
adoQry->ExecSQL();
if(FileExists(txtFilePath))
{
DeleteFile(txtFilePath);
}
参考:http://hi.baidu.com/zyjweb/item/c2db29d5b2adf852d63aae8f
http://msdn.microsoft.com/en-us/library/ms974559.aspx
https://support.office.com/en-us/article/Import-data-from-a-text-file-51965b9e-6fb9-41e7-bcb9-c5c098172582