Search code examples
vb.netado.netsqlclient

How to make a DataTable ReadOnly?


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).


Solution

  • 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.