我用一个StringGrid显示几十条记录,怎样将这些提交回数据库?我用的是
ADO!

解决方案 »

  1.   

    var row:integer;
    begin
    row:=0;
    while not adoquery1.Eof do
    begin
    stringgrid1.rowcount :=stringgrid1.rowcount+1;
    row:=row+1;
    adoquery1.fieldbyname('job_id').asstring:=stringgrid1.Cells[1,row];
    adoquery1.fieldbyname('job_desc').asstring:=stringgrid1.Cells[1,row];
    adoquery1.fieldbyname('min_lvl').asstring:=stringgrid1.Cells[3,row];
    adoquery1.Next;
    end;
    end;
    试试
      

  2.   


    为什么不用dbgrid?if (table1.state=dsEdit) or 9table1.state=dsInsert) then
        table1.post;
      

  3.   

    还是Shiyl(云淡风清)方法好,可以批处理..........
      

  4.   

    那比如:我查询出一些记录在StringGrid中显示,用户修改了其中的一条或是几条记录,怎么保存回数据库?
      

  5.   

    你就把修改后的那个cell里的内容写回数据库里面啊
      

  6.   

    to Shiyl(云淡风清) 那怎么读出那个修改的一条或者是几条记录?
      

  7.   

    你把你改过的那些cell用语句再写回数据集啊,就和把edit里的内容写回数据库一样啊
      

  8.   

    我想一次保存当前StringGrid显示的所有的!怎么办?
      

  9.   

    保存当前StringGrid显示的所有的
    var iLoop: integer;
    begin
      with stringgrid1,tmpadoQuery do
      begin
        For iLoop := 1 to rowcount - 1 do
        begin  
         close;
         sql.text := Format('Insert into yourDbTable      (Field1)values(''%s'')',[cells[1,iloop]]);
         execsql;
         
        end;
        close;
      end;
    end;
      

  10.   

    to lead001() 您的办法好像不行!StringGrid显示的记录可不止一行
      

  11.   

    还是不理解,为什么不用DBStringGrid?
      

  12.   

    写入的问题我解决了,可是新的问题又出现了:
    我用将StringGrid中显示的记录,用ADOQuery往数据库中写入时,出现了下面的问题:StringGrid的RowCount属性在设计时设为15,但在使用时记录数如果没有达到15条,
    写入语句:
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
     i:Integer;
    begin
     for i:=0 to 14 do
      begin
       with ADOQuery1 do
        begin
         Close;
         SQL.Clear;
         SQL.Add('insert into student(s_no,s_name,s_sex) values('+''''+Trim(StringGrid1.Cells[1,i])+''''+','+''''+Trim(StringGrid1.Cells[2,i])+''''+','+''''+Trim(StringGrid1.Cells[3,i])+''''+')');
         ExecSQL;
       end;
     end;
    end;
    那些没有内容的记录也会被写入,写入的是一个空串!我在表中设置了主键,这样就会导致“不能插入重复键”的错误。我的希望是:怎样在写入时避开那些没有StringGrid中没有值的字段?没有内容就写为null值?
      

  13.   

    你用update呢?
    是空串的话你就在作个判断
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
     i:Integer;
    begin
     for i:=0 to 14 do
      begin
    ----------------------------------------------------------------------------------
    if stringgrid1.cells[1,i]<>'' and stringgrid1.cells[2,i]<>'' and stringgrid1.cells[3,i]<>'' then
    begin
    ----------------------------------------------------------------------------------
       with ADOQuery1 do
        begin
         Close;
         SQL.Clear;
         SQL.Add('insert into student(s_no,s_name,s_sex) values('+''''+Trim(StringGrid1.Cells[1,i])+''''+','+''''+Trim(StringGrid1.Cells[2,i])+''''+','+''''+Trim(StringGrid1.Cells[3,i])+''''+')');
         ExecSQL;
       end;
     end;
    end;
    end;
    没有调试你试试,大概意思就是这个