怎样将一个StringGrid显示的所有记录,用ADOQuery一下子写入数据库中?(注意:StringGrid中的记录有的是数据库中已存在的( 存在的记录就用Update)。也就是说写入之前要判断记录是不是已存在的)怎么做?
解决方案 »
- 怎么在Delphi中打开以前用VB保存在Access中的图片
- 如果提取网页中的元素信息
- 十进制小数转换为十六进制小数方法!解答????
- 如何能在一个事件触发后,让Edit控件里的内容自动全选
- Word 问题请教高人.
- 动态库,输入参数中有结构数组,紧跟其后的参数输入错误,地址总是0x0000003b,等待高手。
- dbgrid显示access数据库中的单精度类型的数据问题。
- 请问如何判断该字符串为合法的文件路径?
- 动态绑定tquery与dbgrid控件使用---新手
- 谁知道在控件的原码中加入setparent事件,他会在什么时候触发?
- 各位高手给介绍一下有关Delphi类编程的经验
- 如何将另一窗口中的灰色EDIT控件可编辑!
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的处理能力
另外,该语句最好在一个事务内处理
现在的关键问题就是如何来取得stringgrid中的值,至于 这个,我想应该不用我来说吧,呵呵………………
beginend;
下面 是我当初 做毕业设计的时候的一段代码,如果大家不嫌弃,大家参考,最好能够 给点建议,说说什么地方需要改动
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;