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;

解决方案 »

  1.   

    qry.ExecSQL;
    放循环里面!你放外面当然总是最后一个了!
      

  2.   

    qry.ExecSQL;
    放循环里面!
    那qry.SQL.add( 'insert into test(pic) values (:pic)'); 不用ADD了吧,
    qry.SQL.text := 'insert into test(pic) values (:pic)';,好似效率没有什么提升。
      

  3.   


    你要不放里面根本就不对,而不是效率问题,至于你是用ADD还是Text这是另外的问题!个人感觉小数据量这两种没什么区别。
      

  4.   

    这样修改试试,原代码只能执行(最后)一次。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;
      

  5.   

    提醒: 事务处理的SQL批次不要过多,影响速度和效率。因为事务处理的机制,就是出现错误返回。
      

  6.   

    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; 
      

  7.   

    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.Connection.BeginTrans; 
    qry.ExecSQL;
    qry.Connection.CommitTrans; 
    end;
      

  8.   

    execsql在循环外边,放到里面就行了