with Query do begin Close ; Sql.Text := 'update tablename set field1 = ''1234''' ; ExecSql ; end ;//with DataSet do begin Open ; //... Next ; Edit ; FieldbyName('fieldname').AsString := '' ; Post ; end; 可以吗 ?
你想自己POST,用STRINGGRID不是更好?!
我是在只有一条查询记录的dbgrid控件中直接让用户可以dbgrid单元中直接修改记录内容,然后单击提交button, postclick(sender:object) begin query.post end. 不知为什么,数据库的内容并没有更改
说一下代码:db2表mytable1 dbgrid1,adoquery1,//显示表中所有记录的某些字段,不可编辑 dbgrid2,adoquery2,//双击dbgrid1时则显示这条记录的所有字段,可编辑 若adoquery2.locktype=ltOptimistic,则单击提交时会报错:multiple-step operation generated errors.check each status value 所以我改为adoquery2.locktype=ltBatchOptimistic了,但单击提交mytable1没有任何更改
procedure TForm1.actpostinfoExecute(Sender: TObject); begin dm1.adoquery2.Edit; dm1.adoquery2.post; end;//我的代码,数据库没有任何改变
再添加一条空记录,try try
如何添加一条空记录?或者可以得到修改后的每一个cell中的值吗?
假设你的dbgrid2使用的TDataSource的名为Datasource2 procedure TForm1.actpostinfoExecute(Sender: TObject); begin dm1.Datasource2.DataSet.Edit; dm1.Datasource2.DataSet.Post; end;
procedure TForm1.DBGrid1DblClick(Sender: TObject); var strgonghao,strexamin,strchapter:AnsiString; count:integer; begin if not dm1.adoQuery.Active then exit; if DBGrid1.SelectedRows.Count>=0 then //只会有一行被选中 begin strgonghao:=dm1.adoQuery1.fieldbyname('工号').AsString; dm1.adoquery2.Active:=false; dm1.adoquery2.SQL.Clear(); dm1.adoquery2.SQL.Add('select chrgonghao as 工号,chrname as 姓名,chrdaqu as 大区,chrcity as 城市,decbrowsetime as 浏览时间,intyear as 年,intmonth as 月 '); for count:=0 to intchapter-1 do//intchapter:章节数 strchapter:=Format(strchapter+',intchapterno%d as 第%d章节号,decstudyprogress%d as 第%d章节学习进度,chrchaptername%d as 第%d章节名称,datestartdate%d as 第%d章节开始日期,dateenddate%d as 第%d章节结束日期 ',[count,count,count,count,count,count,count,count,count,count]); dm1.adoInfoquery.SQL.Add(strchapter); for count:=0 to intexamin-1 do//考试数 strexamin:=Format(strexamin+',chrexamincode%d as 考试代号%d,chrexaminname%d as 考试名称%d,decachieve%d as 考试成绩%d,datedate%d as 考试日期%d ',[count,count,count,count,count,count,count,count]); dm1.adoInfoquery.SQL.Add(strexamin); dm1.adoInfoquery.SQL.Add(' from '+strCurrQueryT+' where chrgonghao='''+strgonghao+''''); dm1.adoInfoquery.Open(); end; end;
begin
Close ;
Sql.Text := 'update tablename set field1 = ''1234''' ;
ExecSql ;
end ;//with DataSet do
begin
Open ;
//... Next ;
Edit ;
FieldbyName('fieldname').AsString := '' ;
Post ;
end; 可以吗 ?
postclick(sender:object)
begin
query.post
end.
不知为什么,数据库的内容并没有更改
(2)只有一条记录时,如果只有修改了一个字段或最后一个修改的字段没
有 POST,想办法把光标从最后一个修改的字段上移走,
如:在单击提交button前让光标移动到另一个字段上。
(3)postclick(sender:object)
begin
query.e;dit;
query.post;
end.
begin
dm1.adoInfoquery.Edit;
dm1.adoInfoquery.post;
end;//我的代码,数据库没有任何改变
其实你的问题是不是这样的:
有两个应用程序,两个应用程序都用ADOQuery打开一个表,当一个应用程修改记录,另一个应用程却不能即时更新内容,是吗?
如果这样的话,我想你是用Access数据库,Access确实存在这样的问题,因为我曾碰到过,必须隔一段时候改能接收到数据的修改,所以建议你换数据库。
dbgrid1.DataSource.DataSet.Post;就更新了。
dbgrid1,adoquery1,//显示表中所有记录的某些字段,不可编辑
dbgrid2,adoquery2,//双击dbgrid1时则显示这条记录的所有字段,可编辑
若adoquery2.locktype=ltOptimistic,则单击提交时会报错:multiple-step operation generated errors.check each status value
所以我改为adoquery2.locktype=ltBatchOptimistic了,但单击提交mytable1没有任何更改
begin
dm1.adoquery2.Edit;
dm1.adoquery2.post;
end;//我的代码,数据库没有任何改变
procedure TForm1.actpostinfoExecute(Sender: TObject);
begin
dm1.Datasource2.DataSet.Edit;
dm1.Datasource2.DataSet.Post;
end;
另外:双击dbgrid1时则显示这条记录的所有字段,你的代码是怎么写??我想问题可能出在这里
在按钮事件加入
Edit1.SetFocus;
就ok了
var
strgonghao,strexamin,strchapter:AnsiString;
count:integer;
begin
if not dm1.adoQuery.Active then
exit;
if DBGrid1.SelectedRows.Count>=0 then //只会有一行被选中
begin
strgonghao:=dm1.adoQuery1.fieldbyname('工号').AsString;
dm1.adoquery2.Active:=false;
dm1.adoquery2.SQL.Clear();
dm1.adoquery2.SQL.Add('select chrgonghao as 工号,chrname as 姓名,chrdaqu as 大区,chrcity as 城市,decbrowsetime as 浏览时间,intyear as 年,intmonth as 月 ');
for count:=0 to intchapter-1 do//intchapter:章节数
strchapter:=Format(strchapter+',intchapterno%d as 第%d章节号,decstudyprogress%d as 第%d章节学习进度,chrchaptername%d as 第%d章节名称,datestartdate%d as 第%d章节开始日期,dateenddate%d as 第%d章节结束日期 ',[count,count,count,count,count,count,count,count,count,count]);
dm1.adoInfoquery.SQL.Add(strchapter);
for count:=0 to intexamin-1 do//考试数
strexamin:=Format(strexamin+',chrexamincode%d as 考试代号%d,chrexaminname%d as 考试名称%d,decachieve%d as 考试成绩%d,datedate%d as 考试日期%d ',[count,count,count,count,count,count,count,count]);
dm1.adoInfoquery.SQL.Add(strexamin);
dm1.adoInfoquery.SQL.Add(' from '+strCurrQueryT+' where chrgonghao='''+strgonghao+'''');
dm1.adoInfoquery.Open();
end;
end;
adoInfoquery既是adoquery2,我忘改了
你的代码:dm1.Datasource2.DataSet.Edit;
dm1.Datasource2.DataSet.Post;好象不能连在一起用的哦,先试试在edit与post之间加入一些数据集赋值语句看行不行,如不行则只好把edit与post分别放在不同的事件里执行了。
begin
dm1.adoquery2.Edit;
dm1.adoquery2.post;
end;//我的更新代码,数据库没有任何改变
why???????????????