本人用以下方法将stringgrid中数据写入数据库,但这样如果做,需对stringgrid中每一条记录动态生成sql语句,然后再执行,这样效率慢,各位大哥有更好的方案吗?最好带个例子和代码procedure TxmzdForm.tlbtnSaveClick(Sender: TObject);
var
tempSql: TQuery;
i: integer;
Sqlstr: string;
begin
tempSql:=TQuery.Create(self);
try
tempsql.databaseName:=SystemDataModule.dbSystem.DatabaseName;
tempsql.SQL.Clear;
tempsql.SQL.Add('Delete from xmlszd where xmbh='+quotedstr(trim(edtxmbh.Text)));
tempsql.ExecSQL;
with stglszd do
begin
for i:=FixedRows to RowCount-FixedRows do
begin
tempsql.SQL.Clear;
sqlstr:='Insert into xmlszd (xmbh,zdxh,zdbh,zdmc,zdrq,bz) Values' +
'( '+quotedstr(cells[1,i])+','+quotedstr(cells[0,i])+','+
quotedstr(cells[3,i])+','+quotedstr(cells[4,i])+','+
quotedstr(cells[2,i])+','+quotedstr(cells[5,i])+')';
tempsql.SQL.Add(sqlstr);
tempsql.ExecSQL;
alreadyChange:=false;
end;
end;
finally
tempsql.Close;
tempsql.Free; end;
end;
var
tempSql: TQuery;
i: integer;
Sqlstr: string;
begin
tempSql:=TQuery.Create(self);
try
tempsql.databaseName:=SystemDataModule.dbSystem.DatabaseName;
tempsql.SQL.Clear;
tempsql.SQL.Add('Delete from xmlszd where xmbh='+quotedstr(trim(edtxmbh.Text)));
tempsql.ExecSQL;
with stglszd do
begin
for i:=FixedRows to RowCount-FixedRows do
begin
tempsql.SQL.Clear;
sqlstr:='Insert into xmlszd (xmbh,zdxh,zdbh,zdmc,zdrq,bz) Values' +
'( '+quotedstr(cells[1,i])+','+quotedstr(cells[0,i])+','+
quotedstr(cells[3,i])+','+quotedstr(cells[4,i])+','+
quotedstr(cells[2,i])+','+quotedstr(cells[5,i])+')';
tempsql.SQL.Add(sqlstr);
tempsql.ExecSQL;
alreadyChange:=false;
end;
end;
finally
tempsql.Close;
tempsql.Free; end;
end;
http://community.csdn.net/Expert/topic/3297/3297352.xml?temp=.9591791
然后
for i:=0 to Query.RecordCount-1 do begin
for j:=0 to Query.FieldCount-1 do begin
StringGrid1.Cells[i+1,j+1]:=Query.Fields.Fields[i].Value;
end;
end;
如果是分步式系统,不好用DBGrid,可以用带参数的SQL来插入这样比用SQL要快点