Updating Datatable using Update Query
I am passing string _nameProcessed from the UI to my DAL and updating my table. Now when I am doing the foreach look I could not figure if out how to Update my column ShipmentProcessedBy to _nameProcessed.
Any help guys???
public void SaveNameProcessed(DataTable updatedTable, string _nameProcessed)
{
foreach (DataRow row in updatedTable.Rows)
{
SqlCommand cmd2 = new SqlCommand(
@"update dbo.JobStatus
SET ShipmentProcessedBy = ????,
WHERE JobTableId = @JobID ", _mySqlConnec);
//Updated the parameters to the SQL Query!
cmd2.Parameters.Add开发者_StackOverflow(new SqlParameter("@ProcessedBy", row["ProcessedBy"].ToString()));
cmd2.Parameters.Add(new SqlParameter("@JobID", row["JobID"].ToString()));
cmd2.Connection = _mySqlConnec;
_mySqlConnec.Open();
cmd2.ExecuteNonQuery();
_mySqlConnec.Close();
}
}
Here is the rectified code:
public void SaveNameProcessed(DataTable updatedTable, string _nameProcessed)
{
foreach (DataRow row in updatedTable.Rows)
{
SqlCommand cmd2 = new SqlCommand(
@"update dbo.JobStatus
SET ShipmentProcessedBy = ????,
WHERE JobTableId = @JobID ", _mySqlConnec);
//Updated the parameters to the SQL Query!
cmd2.Parameters.Add(new SqlParameter("@ProcessedBy", _nameProcessed));
cmd2.Parameters.Add(new SqlParameter("@JobID", row["JobID"].ToString()));
cmd2.Connection = _mySqlConnec;
_mySqlConnec.Open();
cmd2.ExecuteNonQuery();
_mySqlConnec.Close();
}
}
You have declared and added a parameter called @ProcessedBy
so just set it to that in your SQL.
SET ShipmentProcessedBy = @ProcessedBy,
Exactly the same as you have done with @JobId
You also need to change this line:
cmd2.Parameters.Add(new SqlParameter("@ProcessedBy", row["ProcessedBy"].ToString()));
to
cmd2.Parameters.Add(new SqlParameter("@ProcessedBy", _nameProcessed));
精彩评论