有关TQuery的查询与插入的问题,有些头痛!
用TQuery,TDataSource和TDBGrid,通过SQL将所查询的记录放在TDBGrid中,另外也通过SQL将记录插入表中,但插入所用的表与查询所用的表是相同的表。
但在不断有记录插入的时候,界面不断地闪烁,有时也出现违例,不知应该怎么办?
查询和插入的代码如下:
void __fastcall TfrmLog::ButtonQueryClick(TObject *Sender)
{
try{
AnsiString s;
AnsiString strQuery;
AnsiString strUser;
AnsiString strType;
AnsiString strSortName;
AnsiString strSortType;
strSortName=ComboBoxSort->Text;
if(RadioButtonASC->Checked)
strSortType="ASC";
else
strSortType="DESC";
if(ComboBoxUser->Text=="All User")
strUser="%";
else strUser=ComboBoxUser->Text;
if(ComboBoxType->Text=="All Type")
strType="%";
else strType=ComboBoxType->Text;
s= "where OperateTime>'"+Edit1->Text+"'" +" And OperateTime <'"+Edit2->Text+"'";
s+=" and UserName like '"+strUser+"'";
s+=" and Type like '"+strType+"'";
strDeleteCondition=s;
s+=" Order by "+strSortName;
s+=" "+strSortType;
strQueryCondition=s;
strQuery="select * from logs "+strQueryCondition;
LogQuery->Close();
LogQuery->SQL->Clear();
LogQuery->SQL->Add(strQuery);
LogQuery->Open();
}
catch(...)
{
ShowMessage("Query DB failed!");
}
}
void __fastcall TfrmLog::InsertLogToDB(PLOGRECORD plr)
{
try{
LogQuery->Close();
LogQuery->SQL->Clear();
LogQuery->SQL->Add("Insert into logs(UserName,Type,Target,Operation) ");
LogQuery->SQL->Add(" values(:UserName,:Type,:Target,:Operation)");
LogQuery->ParamByName("UserName")->AsString=plr->strUser;
LogQuery->ParamByName("Type")->AsString=plr->strType;
LogQuery->ParamByName("Target")->AsString=plr->strTarget;
LogQuery->ParamByName("Operation")->AsString=plr->strOperation;
if(!LogQuery->Prepared)
LogQuery->Prepare();
LogQuery->ExecSQL();
}
catch(...)
{
ShowMessage("Insert a record to DB failed!");
}
}