Question

I am trying to figure out if I need additional logic to avoid a useless call to my SQL db, or if SqlDataAdapter.Update() will do the right thing. If I have this code:

SqlConnection sqlconn = new SqlConnection(connectionString);
sqlconn.Open();
SqlDataAdapter da = new SqlDataAdapter(selectString, sqlconn);
SqlDataTable table = new SqlDataTable();
da.Fill(table);
new SqlCommandBuilder(da);
table.Rows[0][columnName] = 5; // Existing value is already 5
da.Update(table);

Will .Update() still call SQL's UPDATE for that row, or will it not, because no values for the row really changed?

Was it helpful?

Solution

It will call the update statement in SQL, because changing a value in DataSet and setting it to same again, will modify the RowState property.

DataAdapter.Update - MSDN

When an application calls the Update method, the DataAdapter examines the RowState property, and executes the required INSERT, UPDATE, or DELETE statements iteratively for each row, based on the order of the indexes configured in the DataSet.

You can also confirm it using SQL Profiler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top