If I'm updating a DataRow, do I lock the entire DataTable or just the DataRow?
- by Dan Tao
Suppose I'm accessing a DataTable from multiple threads. If I want to access a particular row, I suspect I need to lock that operation (I could be mistaken about this, but at least I know this way I'm safe):
// this is a strongly-typed table
OrdersRow row = null;
lock (orderTable.Rows.SyncRoot) {
row = orderTable.FindByOrderId(myOrderId);
}
But then, if I want to update that row, should I lock the table (or rather, the table's Rows.SyncRoot object) again, or can I simply lock the row?