Normally you will do one update at a time inside a loop. If you have 100 records, you will have 100 trips to the server which is not desirable. How can I update a group of records with a single round trip to the database.
using System.Data.SqlClient;
for (int ii = 0; ii < ptList.Length; ii++) {
sql = @"update [CCVT].[dbo].[_tb_NCVT_Points] set PointDateTime = CONVERT(datetime, '"
+ ptList[ii]._dateDt.ToString("yyyy-MM-dd HH:mm:ss.FFF") + "', 121), PointStatus = '"
+ ptList[ii]._statStr + "', PointValue =" + ptList[ii]._valDoub.ToString()
+ " WHERE Pointkey = '" + ptList[ii]._pointName + "'; ";
theActiveConnection.Open();
SqlCommand cmd = new SqlCommand(sql, theActiveConnection);
try {
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch (Exception eX) {
//handle exceptions
}
}
Please do not down vote this question.
The question How can I update multiple rows in a table with SQL query?
did not ask for one trip, and their answer did not yield one trip!! Did you see the ExecuteNonQuery action is inside the loop?? That is not my question and that is not my answer!
foreach (DataGridViewRow row in dataGridView2.Rows)
{
cm.Parameters["@Qty"].Value = row.Cells[2].Value;
cm.Parameters["@Description"].Value = row.Cells[3].Value;
cm.Parameters["@Price"].Value = row.Cells[4].Value;
cm.ExecuteNonQuery();
}
cn.Close();