Note that there are some explanatory texts on larger screens.

plurals
  1. PODataAdapter.Fill() behavior for row deleted at the data source
    text
    copied!<p>I'm using the <code>DataSet</code>/<code>DataTable</code>/<code>DataAdapter</code> architecture to mediate between the database and my model objects, which have their own backing (they aren't backed by a DataRow). I've got a <code>DataAdapter</code> with <code>AcceptChangesDuringFill = False</code>, <code>AcceptChangesDuringUpdate = False</code>, and <code>FillLoadOption = OverwriteChanges</code>. Here's my understanding of the <code>DataAdapter</code> model under these conditions:</p> <h2>DataAdapter.Update()</h2> <ul> <li><code>DataRowState.Added</code> will result in the <code>InsertCommand</code> firing</li> <li><code>DataRowState.Modified</code> will result in the <code>UpdateCommand</code> firing</li> <li><code>DataRowState.Deleted</code> will result in the <code>DeleteCommand</code> firing</li> </ul> <h2>DataAdapter.Fill()</h2> <ul> <li>Any row in the returned result set whose primary key corresponds to an existing row in the <code>DataTable</code> will be used to update that row, and that row's state will always become <code>DataRowState.Modified</code>, <em>even if the returned row is identical to the current row</em></li> <li>Any row in the returned result set whose primary key doesn't correspond to any existing row will be used to create a new row, and that row's state will become <code>DataRowState.Added</code></li> <li>Any row in the <code>DataTable</code> that doesn't correspond to a row in the returned result set will stay at <code>DataRowState.Unchanged</code></li> </ul> <p>Given that I'm correct with this mental model, suppose I want to use <code>Fill()</code> to notice deleted rows in the data source. Also, suppose that the parameters of the <code>SelectCommand</code> don't return the entire table. I'm guessing that I have two options:</p> <ul> <li>Find all the rows that should've been updated by the <code>Fill()</code> but are still <code>DataRowState.Unchanged</code> (relies on my untested italicized assumption above). These rows have been deleted at the data source.</li> <li>Clear all relevant rows from the <code>DataTable</code> before the <code>Fill()</code>; any row that doesn't show up again has been deleted at the data source. This loses the distinction between <code>DataRowState.Added</code> and <code>DataRowState.Modified</code> that is preserved with the first method.</li> </ul> <p>So, my questions:</p> <ul> <li>Is my above model of the <code>DataAdapter</code> correct, subject to the property values I noted at the top?</li> <li>Which option should I go with to find deleted rows? I'd prefer the first one, but that relies on my assumption that all returned rows will be set to <code>DataRowState.Modified</code> even if the row is identical; is that a safe assumption?</li> <li>Am I going about this all wrong?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload