高分请教一个关于将记录提交回数据库的问题!

zdqyundou 2003-08-31 06:54:01
怎样将一个StringGrid显示的所有记录,用ADOQuery一下子写入数据库中?(注意:StringGrid中的记录有的是数据库中已存在的( 存在的记录就用Update)。也就是说写入之前要判断记录是不是已存在的)怎么做?
...全文
34 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyuer0851 2003-09-01
  • 打赏
  • 举报
回复
呵呵,其实写数据到数据库中我也不会写批量,事务应该能够写…………,

其实还是那样,就和你操作其他的数据库时,插入数据一样的
下面 是我当初 做毕业设计的时候的一段代码,如果大家不嫌弃,大家参考,最好能够 给点建议,说说什么地方需要改动
procedure TForm5.Button1Click(Sender: TObject);
var a_flag :boolean;
i:integer; //i :记录行号,row_no 记录 共有多少条记录
record_no :integer; //记录共录入多少条记录
aflag :boolean; //判断是否是输入的第一条记录
j :integer; //记录覆盖记录的条数
begin
//Form5.StringGrid1.Options[goEditing] :=false;

aflag :=false;
record_no :=0;
j :=0;
if (MessageDlg('确定您的输入?',mtConfirmation,[mbyes,mbNo],0)=mrYes)
then begin
//开始录入学生成绩
//record_no :=0;
With Data_Mod.DataModule1.score_Tab do
begin
close ;
TableName :='score';
active :=true;

for i:=1 to Form5.row_no do
begin
if (Form5.StringGrid1.Cells[2,i]='' )
then begin
showmessage('学生编号为空,请核查');
continue;
end ;
//showmessage(Form5.StringGrid1.Cells[2,i]);
if aflag =false
then begin
a_flag :=Locate('KCID',Form4.kcid ,[loCaseInsensitive]);
if (a_flag =false)
then begin
showmessage('在成绩表中没有该课程记录,不能够录入,请核查');
//exit;
continue ;
end
else begin
// record_No:=0;
form5.StringGrid1.Row:=1;

{if (Form5.StringGrid1.Cells[4,i]<>'' )
then begin
if MessageDlg('学生'+Form5.StringGrid1.Cells[2,i]+Form4.kc_mingcheng+'已经存在,是否覆盖',mtConfirmation,[mbYes,mbNo],0)=mrNo
then continue
else Inc(j);
end; }

a_flag :=Locate('KcId;Id',Vararrayof([Form4.kcid,Form5.StringGrid1.Cells[2,i]]),[loCaseInsensitive]);
if a_flag =true
then begin
if MessageDlg('学生'+Form5.StringGrid1.Cells[2,i]+' 课程'+Form4.kc_mingcheng+' 已经存在,是否覆盖',mtConfirmation,[mbYes,mbNo],0)=mrNo
then continue
else Inc(j);
end
else Inc(record_No);
Edit;
FieldByName('Id').value :=Form5.StringGrid1.Cells[2,i];
FieldByName('score').value :=Form5.StringGrid1.Cells[4,i];
Post;
refresh;

aflag :=true;
end;
end
else begin

{if (Form5.StringGrid1.Cells[4,i]<>'' )
then begin
if MessageDlg('学生'+Form5.StringGrid1.Cells[2,i]+Form4.kc_mingcheng+'已经存在,是否覆盖',mtConfirmation,[mbYes,mbNo],0)=mrNo
then continue
else Inc(j);
end;}
a_flag :=Locate('KcId;Id',Vararrayof([Form4.kcid,Form5.StringGrid1.Cells[2,i]]),[loCaseInsensitive]);
if a_flag =true
then begin
if MessageDlg('学生'+Form5.StringGrid1.Cells[2,i]+' 课程'+Form4.kc_mingcheng+' 已经存在,是否覆盖',mtConfirmation,[mbYes,mbNo],0)=mrNo
then continue
else Inc(j);
end
else Inc(record_No);
Append;
FieldByname('KcId').Value :=Form4.kcid ;
FieldByName('Id').value :=Form5.StringGrid1.Cells[2,i];
FieldByName('score').value :=Form5.StringGrid1.Cells[4,i];
Post;
refresh;


end;
end;
end; //end with
showmessage('录入学生成绩'+inttostr(record_no)+'成功'+#13+#10+'覆盖学生成绩'+inttostr(j)+'成功');
end
else begin
exit;
end;
end;
TripleH 2003-09-01
  • 打赏
  • 举报
回复
to liumengchuan() 写单笔会。这种写批量的还……小弟是初学者啦!
liumengchuan 2003-08-31
  • 打赏
  • 举报
回复
那你会不会把某个数据写到数据库中啊?
liumengchuan 2003-08-31
  • 打赏
  • 举报
回复
如果是大型数据库(比如sqlserver,因为我对它比较熟),支持多语句和流程控制,可以这样写(伪码)
adoquery.sql.clear;
for each row do
begin
adoquery.sql.add("if exists(select * from table where f1= col1 and f2=col2 and ... and fn=coln) then begin");
adoquery.sql.add("update table set f1=col1, ..., fn=coln where f1=col1 and f2=col2 and ... and fn=coln end");
adoquery.sql.add("else being insert table values(col1, col2, ..., coln) end");
end;
adoquery.execsql;

coln代表某一行第n列的值转换为sql语句能接受的值
注意:可能语句会很长,超出sqlserver的处理能力
另外,该语句最好在一个事务内处理
zdqyundou 2003-08-31
  • 打赏
  • 举报
回复
我现在的问题是不知道怎么把StringGrid中的数据写到数据库中,即使不判断是否存在的!
fhuibo 2003-08-31
  • 打赏
  • 举报
回复
while not query.eof do
begin

end;
深圳万兴 2003-08-31
  • 打赏
  • 举报
回复
是的
xiaoyuer0851 2003-08-31
  • 打赏
  • 举报
回复
我做过的,使用stringgrid ,还是和使用其他的一样,首先 取得你最为关键字的那一列的值,然后 进行判断可如果存在则update ,否则进行插入 ,用locate方法进行判断
现在的关键问题就是如何来取得stringgrid中的值,至于 这个,我想应该不用我来说吧,呵呵………………
zdqyundou 2003-08-31
  • 打赏
  • 举报
回复
up

2,497

社区成员

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

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