I am a beginner in Delphi and I need help with the following problem. I have to implement an editable DBGrid on my form. The DBGrid displays results from a query joining 3 tables. I managed to make the DBGrid editable but the edits have no effect on the database. I can insert, edit or delete rows in the DBGrid but these changes do not get saved to the database. I have the following settings:

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 

I am probably missing something but I don't know what. Any help would be appreciated!

有帮助吗?

解决方案

It is claimed that you need several UpdateSQL object, one per table http://docwiki.embarcadero.com/RADStudio/XE3/en/Using_Multiple_Update_Objects_Index

Each of those objects should have base TDataSet bound to them for referencing vanilla data. http://docwiki.embarcadero.com/Libraries/en/Bde.DBTables.TDataSetUpdateObject.DataSet

It is claimed, that ApplyUpdates/Commit/CommitUpdates should be called on different objects in proper order. http://docwiki.embarcadero.com/RADStudio/en/Applying_Cached_Updates_with_Dataset_Component_Methods

PS. BDE is deprecated and badly compatble with recent Windows and recent db servers.

其他提示

// 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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top