[英]Delphi - FireDAC events
我在Delphi中有以下表格:
注意:我正在使用FireDAC數據庫驅動程序和TDBGrid組件。
注意:我正在針對此問題使用測試列名稱和數據。 如果需要,我可以向您發送完整的數據庫表,但是它們在塞爾維亞語中。
這是有效的“ 加載值”事件(組合框)的代碼段:
procedure TForm2.Button4Click(Sender: TObject);
begin
with DataModule1.FDQuery1 do
begin
close;
sql.Clear;
sql.Text:='select distinct Department from protocols';
open;
DataModule1.FDQuery1.First;
while not DataModule1.FDQuery1.Eof do
begin
ComboBox1.Items.Create.Add(DataModule1.FDQuery1['Department']);
DataModule1.FDQuery1.Next;
end;
end;
end;
對於View(記錄) ,以下工作正常:
procedure TForm2.Button2Click(Sender: TObject);
begin
DataModule1.FDQuery1.sql.Text:= ' select protocols.ID_Document, protocols.Document, protocols.ID_Customer, protocols.Customer, protocols.Department, protocols.Date, protocols.Protocol, protocols.ID_Registrator, protocols.Registrator '
+ ' from protocols '
+ ' inner join documents on documents.ID_Document=protocols.ID_Document '
+ ' inner join registrators on registrators.ID_Registrator=protocols.ID_Registrator '
+ ' inner join customers on customers.ID_Customer=protocols.ID_Customer '
+ ' inner join users on users.ID_User=protocols.ID_User ';
DataModule1.FDQuery1.open;
現在,我沒有辦法實現添加新條目,更新(條目記錄),刪除(條目記錄)的方法 。 有人可以演示如何實施這些事件嗎?
您的代碼片段似乎表明您並沒有以正確的方式進行此操作,因為:
a)您正在使用與填充ComboBox1相同的FDQuery來編輯用戶數據。
b)由於Button2Click
中“選擇”中的所有列都來自protocols
表,因此尚不清楚為什么您認為需要任何內部聯接。
c)如果您開始給組件賦予有意義的名稱,而不是ComboBox1
, FDQuery1
等,您將發現調試和繼續編寫應用程序要容易得多。
d)您在q中詢問的內容表明您可能不熟悉Delphi數據集(包括TFDQuery)的工作方式,因為如果您是這樣,那么您根本就不會問這個問題。 我認為您想象您必須實現代碼才能執行插入,更新和刪除操作。 實際上,執行這些操作的功能內置於TFDQuery和其他TDataSet后代中,它們與SQ表一起使用以自動執行這些操作。 我將在下面說明如何執行此操作。
首先試試這個
像這樣重命名您的組件
FDQuery1-> qProtocols ComboBox1-> cbxDepartmente Button4-> btnOpenProtocols Button2-> btnLoadDepartments
向您的數據模塊添加一個額外的TFDQuery並調用它,例如qDepartmentList
如下所示更改代碼。
碼:
procedure TForm1.btnLoadDepartmentsClick(Sender: TObject);
begin
// Re-written do avoid using "With ..."
if DataModule1.qDepartmentList.Active then
DataModule1.qDepartmentList.Close;
DataModule1.qDepartmentList.Sql.Text := 'select distinct Department from protocols';
DataModule1.qDepartmentList.Open;
// DataModule1.FDQuery1.First; <_ You DON'T need First, because the call to Open does that
cbxDepartmentList.Items.Clear; // Clear the combo if it's already populated
while not DataModule1.qDepartmentList.Eof do begin
// ComboBox1.Items.Create.Add(DataModule1.FDQuery1['Department']);
// You don't need ad should not call Create in the above
cbxDepartmentList.Items.Add(DataModule1.qDepartmentList['Department']);
DataModule1.qDepartmentList.Next;
end;
end;
procedure TForm1.btnOpenProtocolsClick(Sender: TObject);
begin
DataModule1.qProtocols.Sql.Text:= ' select protocols.ID_Document, protocols.Document, protocols.ID_Customer, protocols.Customer, protocols.Department, protocols.Date, protocols.Protocol, protocols.ID_Registrator, protocols.Registrator '
+ ' from protocols '
+ ' inner join documents on documents.ID_Document=protocols.ID_Document '
+ ' inner join registrators on registrators.ID_Registrator=protocols.ID_Registrator '
+ ' inner join customers on customers.ID_Customer=protocols.ID_Customer '
+ ' inner join users on users.ID_User=protocols.ID_User ';
DataModule1.qProtocols.Open;
end;
此時,請暫停並檢查您是否仍然可以編譯代碼。 請不是我添加了關於“用”的評論- with
原因遠遠更多的麻煩比它的價值。
然后:
添加以下ButtonClick處理程序:
過程TForm1.btnInsertClick(Sender:TObject); 開始DataModule1.qProtocols.Insert; 結束;
過程TForm1.btnSaveChangesClick(Sender:TObject); 開始DataModule1.qProtocols.Delete; 結束;
過程TForm1.btnDeleteClick(Sender:TObject); 開始DataModule1.qProtocols.Delete; 結束;
在聯機幫助中查看TDataSet的Insert,Post和Delete方法的作用。
至此,我們快完成了。 剩下要做的唯一事情就是處理將編輯控件的內容移入和移出qProtocols。 如果您使用了TEdit的可識別數據庫的版本,即TDBEDit,則DBEdits將自動執行這兩項操作。 如果您使用過TEdits-我認為這是一個錯誤,您將需要將數據從TEdits的Text屬性復制,還將cbxDepartmentList復制到qProtocols記錄的字段中。 最好的選擇可能是在qProtocols AfterInsertEvent中。
如果您希望我添加一些代碼來使用TEdit,可以,但是如果您使用TDBEdits而不是TEdits和TDBComboBox而不是TComboBox,則可以節省大量的代碼編寫,調試和維護代碼的時間。
我將留給您研究在線幫助,以研究如何實現CancelChanges功能。 另外,您可能會發現將TDBNavigator添加到與DBGrid連接到同一TDataSource的窗體上很有啟發性,因為它顯示了TButton的所有功能以及更多功能。 如您所見,它還會根據是瀏覽還是編輯數據集來自動啟用和禁用某些速度按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.