使用更新查询更新数据表

时间:2010-12-16 20:35:11

标签: sql

我将字符串_nameProcessed从UI传递到我的DAL并更新我的表。现在当我在做foreach时,我无法弄清楚如何将我的列ShipmentProcessedBy更新为_nameProcessed。

任何帮助人员???

 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", row["ProcessedBy"].ToString()));
            cmd2.Parameters.Add(new SqlParameter("@JobID", row["JobID"].ToString()));

            cmd2.Connection = _mySqlConnec;
            _mySqlConnec.Open();
            cmd2.ExecuteNonQuery();
            _mySqlConnec.Close();
        }

    }

以下是经过纠正的代码:

 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();
        }

    }

1 个答案:

答案 0 :(得分:0)

您已声明并添加了一个名为@ProcessedBy的参数,因此只需将其设置为SQL中的参数即可。

SET ShipmentProcessedBy = @ProcessedBy,

@JobId

完全相同

您还需要更改此行:

cmd2.Parameters.Add(new SqlParameter("@ProcessedBy", row["ProcessedBy"].ToString()));

cmd2.Parameters.Add(new SqlParameter("@ProcessedBy", _nameProcessed));