你用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;
没有调试你试试,大概意思就是这个
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显示的所有的
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;
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;
试试