qry:=TAdoQuery.Create(nil);
qry.Connection :=conn;
qry.Connection.BeginTrans;
for i:=0 to TFilePathList.count-1do
begin
strm.LoadFromFile(TFilePathList[i]);
qry.SQL.add( 'insert into test(pic) values (:pic)');
qry.Parameters.ParseSQL(qry.SQL.Text ,true);
qry.Parameters.ParamByName('pic').LoadFromStream(strm,ftBlob);
end;
qry.ExecSQL;
qry.Connection.CommitTrans;
為什么上面總是存盤最后一條的資料?
上面的存盤若使用事務(下面是參考sqlserver事務),紅色字體是我使用事務,還是不行,各位大神有沒有思路?
for i:=1 to 10000 do
begin
Name:= QuotedStr('easyboot'+InttoStr(i));
SqlStr:=SqlStr+ Format(' insert into MyTest (Name) values (%s) ',[Name]);
end;
ADOConnection1.Open;
ADOConnection1.BeginTrans;
ADOCommand1.CommandText:=SqlStr;
ADOCommand1.Execute();
ADOConnection1.CommitTrans;
uj5u.com熱心網友回復:
qry.ExecSQL;放回圈里面!你放外面當然總是最后一個了!
uj5u.com熱心網友回復:
qry.ExecSQL;放回圈里面!
那qry.SQL.add( 'insert into test(pic) values (:pic)'); 不用ADD了吧,
qry.SQL.text := 'insert into test(pic) values (:pic)';,好似效率沒有什么提升。
uj5u.com熱心網友回復:
你要不放里面根本就不對,而不是效率問題,至于你是用ADD還是Text這是另外的問題!個人感覺小資料量這兩種沒什么區別。
uj5u.com熱心網友回復:
這樣修改試試,原代碼只能執行(最后)一次。qry.Connection.BeginTrans;
for i:=0 to TFilePathList.count-1do
begin
strm.LoadFromFile(TFilePathList[i]);
qry.SQL.add( 'insert into test(pic) values (:pic)');
qry.Parameters.ParseSQL(qry.SQL.Text ,true);
qry.Parameters.ParamByName('pic').LoadFromStream(strm,ftBlob);
qry.ExecSQL;
end;
qry.Connection.CommitTrans;
比較好的事務處理,代碼如下:
try
ADOCon.BeginTrans; // 開始事務
for i:=0 to n do
begin
SQL:='InSetrt.......'; // 生成SQL陳述句
ADOCon.Execute(SQL); // 放入執行佇列
end;
ADOCon.CommitTrans; // 提交事務
Result:=True;
except
on E:Exception do
begin
ADOCon.RollbackTrans; // 事務回滾
err:=E.Message;
end;
end;
uj5u.com熱心網友回復:
提醒: 事務處理的SQL批次不要過多,影響速度和效率。因為事務處理的機制,就是出現錯誤回傳。uj5u.com熱心網友回復:
qry:=TAdoQuery.Create(nil);qry.Connection :=conn;
for i:=0 to TFilePathList.count-1do
begin
strm.LoadFromFile(TFilePathList[i]);
qry.SQL.add( 'insert into test(pic) values (:pic)');
qry.Parameters.ParseSQL(qry.SQL.Text ,true);
qry.Parameters.ParamByName('pic').LoadFromStream(strm,ftBlob);
end;
//其實,事務在這里有意義更大的吧!
//讓服務器去回應你的事務,分配資源,一個事務的體積也不建議太大
//覺得這樣的效率會不會更高點
qry.Connection.BeginTrans;
qry.ExecSQL;
qry.Connection.CommitTrans;
uj5u.com熱心網友回復:
for i:=0 to TFilePathList.count-1dobegin
strm.LoadFromFile(TFilePathList[i]);
qry.SQL.add( 'insert into test(pic) values (:pic)');
qry.Parameters.ParseSQL(qry.SQL.Text ,true);
qry.Parameters.ParamByName('pic').LoadFromStream(strm,ftBlob);
//上面寫錯了,修改一下
//其實,事務在這里有意義更大的吧!
//讓服務器去回應你的事務,分配資源,一個事務的體積也不建議太大
//覺得這樣的效率會不會更高點
qry.Connection.BeginTrans;
qry.ExecSQL;
qry.Connection.CommitTrans;
end;
uj5u.com熱心網友回復:
execsql在回圈外邊,放到里面就行了轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/72925.html
標籤:數據庫相關
上一篇:csdn論壇怎樣搜索啊?
