【发布时间】:2021-02-27 08:35:38
【问题描述】:
我想知道为什么我的 DBGridEh(后代 DBGrid)在我使用以下 MRE 删除数据库中的一条记录后没有刷新。您会在下面发现我的 4 次尝试,但没有运气。删除基本有效,但 DBGrid 不会更新或与数据源断开连接。
procedure TForm9.btnDeleteClick(Sender: TObject);
begin
//cds1.DisableControls; //attempt 1 failed. It deletes the record but not changes on the DBGrid
uq1.SQL.Clear;
uq1.SQL.Text := 'DELETE FROM mymachine WHERE ListID = :Pid';
uq1.Params.ParamByName('Pid').Value := cds1.FieldByName('ListID').AsInteger;
uq1.ExecSQL;
//cds1.EnableControls; //attempt 1 failed. It deletes the record but not changes on the DBGrid
//cds1.Active := False; //attempt 2 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Active := True; //attempt 2 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Close; //attempt 3 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Open; //attempt 3 failed. "SQL Statement doesn't return rows." but it deletes and cleared the data on the DBGrid (seemed disconnected from datasource)
//cds1.Refresh; //attempt 4 failed. "SQL Statement doesn't return rows." but it deletes and DBGrid data remains
end;
更新 VCL.FILE
object dgh1: TDBGridEh
object ucn1: TUniConnection
ProviderName = 'mySQL'
Port = 3306
Database = 'manufacturingmngtsystem'
Username = 'root'
Server = 'localhost'
Connected = True
LoginPrompt = False
Left = 16
Top = 200
end
object mup1: TMySQLUniProvider
end
object uq1: TUniQuery
Connection = ucn1
SQL.Strings = (
'Select * From mymachine')
end
object dsp1: TDataSetProvider
DataSet = uq1
end
object cds1: TClientDataSet
Active = True
Aggregates = <>
Params = <>
ProviderName = 'dsp1'
object intgrfldcds1ListID: TIntegerField
FieldName = 'ListID'
end
object strngfldcds1Name: TStringField
FieldName = 'Name'
Required = True
Size = 36
end
object strngfldcds1Description: TStringField
FieldName = 'Description'
Size = 209
end
object strngfldcds1Status: TStringField
FieldName = 'Status'
Required = True
FixedChar = True
Size = 8
end
end
object ds1: TDataSource
DataSet = cds1
end
end
【问题讨论】:
-
如果您的 DBGridEh 连接到
cds1,您为什么希望网格更新?您的代码现在所做的一切都不会影响cds1的内容,那么为什么网格显示的内容会发生变化?无论如何,如果你需要这方面的帮助,请说出 a) 你的 uq1 是什么数据类型,b) 你的 uq1 连接到什么 SQL 数据库类型。 -
有什么反对只打电话给
cds1.Delete吗? -
您应该使用评论中询问的详细信息更新您的问题,而不是在您的答案中添加评论。这就是增强问题的方式。我认为当 MartynA 问“你为什么希望网格更新”时,他不是在问用户为什么希望看到网格更新,而是在问“你的代码中的什么会使网格更新”。他解释了为什么......
-
我的意思是你的
uq1是什么Delphi组件类型。此外,您使用哪种 Delphi 组件类型从 mysql 数据库获取数据到cds1。我问这些事情是因为您似乎很可能以完全错误的方式进行 thid=s 。顺便说一句,哪个 Delphi 版本? -
根据您对@UweRaabe 所说的话,您之前似乎错过了使用
CDS和TDataSetProvider处理SQL 数据库时,您只需执行您想要对CDS执行的操作并调用其ApplyUpdates方法将更改传播回您的SQL 数据库。您根本不需要做诸如使用uq1之类的事情。自己谷歌一个关于使用 TDataSetProvider 和 TClientDataSet 的教程并学习它。
标签: delphi tclientdataset dbgrid