在C#中使用两个条件更新SQL语句

时间:2018-06-08 01:48:20

标签: c# sql sql-update

如何在where子句中放入两个条件。 "&类标识码#34;是其中一个条件

pendinglist.CommandText = " UPDATE Pending_List set room_status =" +
    "Still Pending" ; " Where Class_ID = 

2 个答案:

答案 0 :(得分:0)

您应该在and子句中使用where

   pendinglist.CommandText = " UPDATE Pending_List set room_status =" +
  "'Still Pending'" + " Where Class_ID = "+ "[your_id]" + " and room_status 
!= 'Still Pending'"

答案 1 :(得分:0)

请使用Prepared Statements查找以下代码,以便在SQL Server实例上创建该命令的准备版本。

private static void SqlCommandPrepareEx(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand(null, connection);

        // Create and prepare an SQL statement.
        command.CommandText =
            "UPDATE Pending_List set room_status=@roomStatus " +
            "WHERE Class_ID=@classId AND Other=@Other";
        SqlParameter classIdParam = new SqlParameter("@classId", SqlDbType.Int, 0);
        SqlParameter OtherParam = new SqlParameter("@OtherParameter", SqlDbType.Int, 0);
        SqlParameter roomStatusParam = 
            new SqlParameter("@roomStatus", SqlDbType.Text, 100);
        classIdParam.Value = 1;
        roomStatusParam.Value = "Still Pending";
        otherParam.Value = 10;
        command.Parameters.Add(classIdParam);
        command.Parameters.Add(roomStatusParam);
        command.Parameters.Add(otherParam);

        // Call Prepare after setting the Commandtext and Parameters.
        command.Prepare();
        command.ExecuteNonQuery();    
    }
}