说明:
1.kys 为数据表 包括(BH,MC,SL)三个字段
2.我要把 StringGrid1 的 东东加入 kys 表中
var
i,j:integer;
bhstr,mcstr:string;
SLINT:INTEGER;
begin
    //form1.Query1.Close;
    //form1.Query1.sql.Clear;
    for i:= 1 to  form1.StringGrid1.rowCount -1 do
    begin
      form1.Query1.Close;
      form1.Query1.sql.Clear;
      bhstr:=form1.StringGrid1.Cells[i,1];
      mcstr:=form1.StringGrid1.Cells[i,2];
      SLINT:=form1.StringGrid1.Cells[i,3];  
      form1.Query1.SQL.Add('insert into kys (BH,MC,SL) values (bhstr,mcstr,SLINT)');  //增加
      form1.Query1.ExecSQL;
   end;
   //form1.Query1.ExecSQL;
end;
为什么 不对?谢谢了

解决方案 »

  1.   

    你用参数传递 的方法不对, 
          form1.Query1.Close;
          form1.Query1.sql.Clear;      
          form1.Query1.SQL.Add('insert into kys (BH,MC,SL) values (:bhstr,:mcstr,:SLINT)');  //增加
        for i:= 1 to  form1.StringGrid1.rowCount -1 do
        begin
         form1.Query1.ParamByName('bhstr').Value := form1.StringGrid1.Cells[i,1];
         form1.Query1.ParamByName('mcstr').Value := form1.StringGrid1.Cells[i,2];
         form1.Query1.ParamByName('SLINT').Value := form1.StringGrid1.Cells[i,3];
         form1.Query1.ExecSQL;
         form1.Query1.Close;
        end;
      

  2.   

    当你的form1.StringGrid1.rowCount =2时,即 for循环只运行一次,应该不会出错,但,form1.StringGrid1.rowCount >2就不能保证了,原因,你在form1.Query1.ExecSQL,执行后循环又立即执行form1.Query1.Close,这是不合法的因为execsql执行是有时间的,(进程的关系)本来你的写法就应该说不对,为什么不用
    Query1.Append;???
      

  3.   

    form1.Query1.SQL.Add('insert into kys (BH,MC,SL) values (bhstr,mcstr,SLINT)');  这条语句也错了,晕bhstr,mcstr,SLINT 不能放在单引号内啊,都成字符了
    你要把值传进去啊sblstr :string;
    sblstr :='insert into kys(BH,MC,SL) values('''+bhstr+''','''+mcstr+''','+SLINT+')';
      

  4.   

    form1.Query1.Close;
          form1.Query1.sql.Clear;      
          form1.Query1.SQL.Add('insert into kys (BH,MC,SL) values (:bhstr,:mcstr,:SLINT)');  //增加
        for i:= 1 to  form1.StringGrid1.rowCount -1 do
        begin
         form1.Query1.ParamByName('bhstr').Value := form1.StringGrid1.Cells[i,1];
         form1.Query1.ParamByName('mcstr').Value := form1.StringGrid1.Cells[i,2];
         form1.Query1.ParamByName('SLINT').Value := form1.StringGrid1.Cells[i,3];
         form1.Query1.ExecSQL;
         form1.Query1.Close;
        end;