سؤال

I am writing a function that return a DataTable object. I am using the DataTable.Load( DataReader) to populate the DataTable. However, as I understand it, although the underlying DataReader is ReadOnly, Forward-only; once it is loaded into the DataTable; other programmers can still update the data by DataTable.Rows(x).Column(y).ReadOnly = False and then setting the value.

Is there away to ensure that the DataTable that my function return is ReadOnly? In the older ADODB days, I use the ADODB.LockTypeEnum to mark the returned recordset is readonly.

By ReadOnly, I mean that the caller to my function (who get the returned DataTable) cannot make changes to the Rows data and/or update it at all (ie, readonly).

هل كانت مفيدة؟

المحلول

Is there away to ensure that the DataTable that my function return is ReadOnly?

No, generally there is no such method or way to make it readonly. Unlike the DataViewwhich has the property AllowEdit for this purpose( so you could return a DataView instead).

The only way i see to avoid changes on the table is: try to handle the RowChanged event and call RejectChanges afterwards.

نصائح أخرى

Another approach to prevent a caller from corrupting your internal DataTable (or DataSet) is to return a copy. Both the DataSet and DataTable classes have a Copy() method that duplicates both the structure and data of the original.

Obviously, this will work better with DataTables containing relatively small amounts of data.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top