我是Delphi的初学者,我需要以下问题的帮助。我必须在我的表单上实现一个可编辑的DBGrid。 DBGrid显示连接3个表的查询的结果。我设法使DBGrid可编辑,但编辑对数据库没有影响。我可以在DBGrid中插入,编辑或删除行,但这些更改不会保存到数据库中。我有以下设置:
Query2.DatabaseName=Test
Query2.SQL=SELECT cd.hourstart, cd.hourfinish, o.objname, cd."work", cd.worktime
FROM Card c JOIN CardDetail cd ON c.N=cd.card
JOIN objects o ON cd.project=o.N
WHERE c.worker=5 AND c.data=CONVERT(DATE, GETDATE())
Query2.UpdateObject=UpdateSQL2
Query2.CachedUpdates=True
Query2.RequestLive=True
UpdateSQL2.DeleteSQL=delete from CardDetail
where hourstart = :OLD_houerstart and
hourfinish = :OLD_houerfinish and
work = :OLD_work and
worktime = :OLD_worktime
UpdateSQL2.InsertSQL=...
UpdateSQL2.ModifySQL=...
DataSource2.DataSet=Query2
DBGrid2.DataSource=DataSource2
我可能遗漏了一些东西,但我不知道是什么。任何帮助将不胜感激!
答案 0 :(得分:2)
// Have a look at the below code, it uses an AdoCommand
procedure DoSomething(DataSet: TDataSet);
var
ADOCommand : TADOCommand;
begin
ADOCommand:=TADOCommand.Create(nil);
try
try
ADOCommand.Connection:=Conn;
ADOCommand.Parameters.Clear;
Conn.BeginTrans;
if dataset.State = dsinsert then
begin
ADOCommand.CommandText := //Your insert statement
ADOCommand.ParamCheck:=False;
ADOCommand.Execute;
end
else
if DataSet.State = dsEdit then
begin
ADOCommand.CommandText := //Your edit statement
ADOCommand.ParamCheck:=False;
ADOCommand.Execute;
end
else
Begin
ADOCommand.CommandText := //Your delete statement
ADOCommand.ParamCheck:=False;
ADOCommand.Execute;
End;
Conn.CommitTrans;
except
on E : Exception do
begin
Conn.RollbackTrans;
MessageDlg('An exception occured, Error Class: ' + E.ClassName +#13+ 'ErrorMessage: '+ E.Message , mtError, [mbOK], 0);
Abort;
end;
end;
finally
ADOCommand.Free;
end;
end;
答案 1 :(得分:1)
据称你需要几个UpdateSQL对象,每个表一个 http://docwiki.embarcadero.com/RADStudio/XE3/en/Using_Multiple_Update_Objects_Index
每个对象都应该绑定基础TDataSet以引用vanilla数据。 http://docwiki.embarcadero.com/Libraries/en/Bde.DBTables.TDataSetUpdateObject.DataSet
据称,应该按正确的顺序在不同的对象上调用ApplyUpdates / Commit / CommitUpdates。 http://docwiki.embarcadero.com/RADStudio/en/Applying_Cached_Updates_with_Dataset_Component_Methods
PS。 BDE已被弃用,并且与最近的Windows和最近的数据库服务器非常兼容。