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; 试试
为什么不用dbgrid?if (table1.state=dsEdit) or 9table1.state=dsInsert) then table1.post;
保存当前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;
to lead001() 您的办法好像不行!StringGrid显示的记录可不止一行
还是不理解,为什么不用DBStringGrid?
写入的问题我解决了,可是新的问题又出现了: 我用将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值?
你用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; 没有调试你试试,大概意思就是这个
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;
试试
为什么不用dbgrid?if (table1.state=dsEdit) or 9table1.state=dsInsert) then
table1.post;
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;
我用将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值?
是空串的话你就在作个判断
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;
没有调试你试试,大概意思就是这个