switch语句中的查询集未执行

时间:2015-04-13 10:35:27

标签: c#

我正在尝试执行switch语句中设置的两个查询,但似乎都没有更新数据库:

switch (status) {
    case "Trade Rejected":
        query = "UPDATE booking " +
            "SET status=@status " +
            "WHERE (id=@job_offer_id AND id=@job_received_id);" +
            "UPDATE trade " +
            "SET job_received_id=0 " +
            "WHERE id=@id";
        break;

    case "Trade Accepted":
        query = "UPDATE booking " +
            "SET status=@status " +
            "WHERE (id=@job_offer_id AND id=@job_received_id);";
        query += "UPDATE booking AS offer " +
            "JOIN booking AS received " +
            "SET offer.operator_id = received.operator_id, received.operator_id = offer.operator_id " +
            "WHERE offer.id=@job_offer_id and received.id=@job_received_id";
        break;
};

我认为代码是在switch语句中设置的,但是没有被执行。什么是处理这个问题的最佳方法。我应该恢复使用if else语句吗?

if (jobList.Count <= 0) return;
foreach(JobComponent.JobList job in jobList) {
    string[] statusArray = {
        "Trade Accepted", "Trade Rejected"
    };
    var status = statusArray[Rnd.Next(statusArray.Length)];

    switch (status) {
        case "Trade Rejected":
            query = "UPDATE booking " +
                "SET status=@status " +
                "WHERE (id=@job_offer_id AND id=@job_received_id);" +
                "UPDATE trade " +
                "SET job_received_id=0 " +
                "WHERE id=@id";
            break;

        case "Trade Accepted":
            query = "UPDATE booking " +
                "SET status=@status " +
                "WHERE (id=@job_offer_id AND id=@job_received_id);";
            query += "UPDATE booking AS offer " +
                "JOIN booking AS received " +
                "SET offer.operator_id = received.operator_id, received.operator_id = offer.operator_id " +
                "WHERE offer.id=@job_offer_id and received.id=@job_received_id";
            break;
    };

    using(var cmd = new MySqlCommand(query, DbObject.Connection)) {
        if (DbObject.Connection.State != ConnectionState.Open) {
            DbObject.OpenConnection();
        }
        try {
            cmd.Parameters.AddWithValue(("@id"), job.JobTradeId);
            cmd.Parameters.AddWithValue(("@job_offer_id"), job.JobOfferId);
            cmd.Parameters.AddWithValue(("@job_received_id"), job.JobReceivedId);
            cmd.Parameters.AddWithValue(("@status"), status);
            cmd.ExecuteNonQuery();
        } catch (Exception ex) {
            ErrorHandlingComponent.LogError(ex.ToString());
            throw;
        }
    }
    DbObject.CloseConnection();
}

1 个答案:

答案 0 :(得分:0)

想出来 - SQL查询存在问题:

                 query = "UPDATE booking " +
                         "SET status=@status " +
                         "WHERE (id=@job_offer_id AND id=@job_received_id);"

应该是

                    query = "UPDATE booking " +
                            "SET status=@status " +
                            "WHERE id=@job_offer_id;" +

                            "UPDATE booking " +
                            "SET status=@status " +
                            "WHERE id=@job_received_id;";

或CodeCaster建议:

WHERE (id=@job_offer_id OR id=@job_received_id);