Delphi ClientDataset Read-only

心不动则不痛 提交于 2019-12-10 20:48:07

问题


I am currently testing with:

  1. A SQLConnection which is pointed towards an IB database.
  2. A SQLDataset that has a SQLConnection field set to the one above.
  3. A DatasetProvider that has the SQLDataset in (2) as its Dataset field value.
  4. A ClientDataset, with the ProviderName field pointing to the provider in (3).

I use the following method (borrowed from Alister Christie) to get the data...

function TForm1.GetCurrEmployee(const IEmployeeID: integer): OleVariant; 
const 
  SQLSELEMP = 'SELECT E.* FROM EMPLOYEE E WHERE E.EMPLOYEEID = %s'; 
begin 
  MainDM.SQLDataset1.CommandText := Format(SQLSELEMP, [Edit1.Text]); 
  Result := MainDM.DataSetProvider1.Data; 
end;

Which populates the DBGrid with just one record. However, when I manually edit the record, click on Post, then try to commit the changes, using

MainDM.ClientDataset1.ApplyUpdates(0); // <<<<<< 

It bombs, with the message "SQLDataset1: Cannot modify a read-only dataset."

I have checked the ReadOnly property of the Provider, and of the ClientDataset, and the SQL has no joins.

What could be causing the error?


回答1:


It appears that your ClientDataSet.Data property is being populated from the Data property of the DataSetProvider. With the setup you described, you should be able to simply call ClientDataSet.Open, which will get the data from the DataSetProvider.

BTW, the default behavior of the DataSetProvider when you call the ClientDataSet.ApplyUpdates method is to send a SQL query to the connection object, and not the DataSet from which the data was obtained (assuming a homogeneous query). Make sure that your DataSetProvider.ResolveToDataSet property is not set to true.

Finally, on an unrelated note, your code above appears to be open to a SQL injection attack (though I have not tested this). It is safer to use a parameter to define the WHERE clause. If someone enters the following into Edit1 you might be in trouble (assuming the InterBase uses the drop table syntax): 1;drop table employee;




回答2:


Check the LiveMode property of the TIBDataSet.



来源:https://stackoverflow.com/questions/653350/delphi-clientdataset-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!