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

TripleH 2003-08-26 06:50:19
我用一个StringGrid显示几十条记录,怎样将这些提交回数据库?我用的是
ADO!
...全文
78 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
TripleH 2003-09-01
  • 打赏
  • 举报
回复
我想一次保存当前StringGrid显示的所有的!怎么办?
Shiyl 2003-09-01
  • 打赏
  • 举报
回复
你把你改过的那些cell用语句再写回数据集啊,就和把edit里的内容写回数据库一样啊
Shiyl 2003-09-01
  • 打赏
  • 举报
回复
你用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;
没有调试你试试,大概意思就是这个
TripleH 2003-09-01
  • 打赏
  • 举报
回复
写入的问题我解决了,可是新的问题又出现了:
我用将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值?
890752 2003-09-01
  • 打赏
  • 举报
回复
还是不理解,为什么不用DBStringGrid?
TripleH 2003-09-01
  • 打赏
  • 举报
回复
to lead001() 您的办法好像不行!StringGrid显示的记录可不止一行
lead001 2003-09-01
  • 打赏
  • 举报
回复
保存当前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;
XXSingle 2003-08-26
  • 打赏
  • 举报
回复
还是Shiyl(云淡风清)方法好,可以批处理..........
TripleH 2003-08-26
  • 打赏
  • 举报
回复
to Shiyl(云淡风清) 那怎么读出那个修改的一条或者是几条记录?
chl_ccssddnn 2003-08-26
  • 打赏
  • 举报
回复

为什么不用dbgrid?

if (table1.state=dsEdit) or 9table1.state=dsInsert) then
table1.post;
Shiyl 2003-08-26
  • 打赏
  • 举报
回复
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;
试试
cow8063 2003-08-26
  • 打赏
  • 举报
回复
你怎么显示的,就怎么读回来呀..........
一格一格来了
xx9yk 2003-08-26
  • 打赏
  • 举报
回复
批处理可以用CachedUpdate
Shiyl 2003-08-26
  • 打赏
  • 举报
回复
你就把修改后的那个cell里的内容写回数据库里面啊
TripleH 2003-08-26
  • 打赏
  • 举报
回复
那比如:我查询出一些记录在StringGrid中显示,用户修改了其中的一条或是几条记录,怎么保存回数据库?

2,496

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧