为什么我的更新语句没有更新表?

时间:2012-03-19 18:08:44

标签: c# .net oracle sql-update

它编译,似乎运行,但记录没有改变。

这是我的代码:

private void UpdateRecord(string ATicketID, string ATicketSource, string AContactsEmail, string AAboutSomeID, string ACategoryID)
{
    try
    {
        con = new OracleConnection(oradb);
        con.Open();

        String query = "UPDATE ABC.CONCERTTICKETS SET TICKETSOURCE = :p_TICKETSOURCE, ABOUTSOMEID = :p_ABOUTSOMEID, CATEGORYID = :p_CATEGORYID, CONTACTEMAIL = :p_CONTACTEMAIL WHERE TICKETID = :p_TICKETID";

        cmd = new OracleCommand(query, con);
        cmd.CommandType = CommandType.Text;

        OracleParameter p_TICKETID =
            new OracleParameter("p_TICKETID", OracleDbType.NVarchar2, ParameterDirection.Input);
        p_TICKETID.Size = 20;
        p_TICKETID.Value = ATicketID;
        cmd.Parameters.Add(p_TICKETID);

        OracleParameter p_TICKETSOURCE =
            new OracleParameter("p_TICKETSOURCE", OracleDbType.NVarchar2, ParameterDirection.Input);
        p_TICKETSOURCE.Size = 20;
        p_TICKETSOURCE.Value = ATicketSource;
        cmd.Parameters.Add(p_TICKETSOURCE);

        OracleParameter p_ABOUTSOMEID =
            new OracleParameter("p_ABOUTSOMEID", OracleDbType.Int32, ParameterDirection.Input);
        p_ABOUTSOMEID.Value = AAboutSOMEID;
        cmd.Parameters.Add(p_ABOUTSOMEID);

        OracleParameter p_CATEGORYID =
            new OracleParameter("p_CATEGORYID", OracleDbType.Int32, ParameterDirection.Input);
        p_CATEGORYID.Value = ACategoryID;
        cmd.Parameters.Add(p_CATEGORYID);

        OracleParameter p_CONTACTEMAIL =
            new OracleParameter("p_CONTACTEMAIL", OracleDbType.NVarchar2, ParameterDirection.Input);
        p_CONTACTEMAIL.Size = 100;
        p_CONTACTEMAIL.Value = AContactsEmail;
        cmd.Parameters.Add(p_CONTACTEMAIL);

        try
        {
            try
            {
                ot = con.BeginTransaction();
                cmd.Transaction = ot;
                cmd.ExecuteNonQuery();
                ot.Commit();
            }
            catch (Exception)
            {
                ot.Rollback();
            }
        }
        catch (OracleException ex)
        {
            MessageBox.Show(ex.Message);
        }
        MessageBox.Show("Apparent success");
    }
    finally
    {
        con.Close();
        con.Dispose();
    }

    dataGridView1.Refresh();
}

====== 更新:

所以你的意思是这样,我接受它:

try
                {
                    using (var transaction = con.BeginTransaction())
                {
                    cmd.Transaction = transaction;
                    cmd.ExecuteNonQuery();
                    transaction.Commit();
                } 
                }                
                catch (Exception ex)
                {
                    ot.Rollback();
                    throw;
                }
                MessageBox.Show("Apparent success");

=========== 再次更新(此代码有效):

private void UpdateRecord(string ATicketID, string ATicketSource, string AContactsEmail, string AAboutSomeID, string ACategoryID)
{
    try
    {
        con = new OracleConnection(oradb);
        con.Open();

        String update = @"UPDATE ABC.CONCERTTICKETS 
                          SET TICKETSOURCE = :p_TICKETSOURCE, 
                          ABOUTSOMEID = :p_ABOUTSOMEID, 
                          CATEGORYID = :p_CATEGORYID, 
                          CONTACTEMAIL = :p_CONTACTEMAIL 
                          WHERE TICKETID = :p_TICKETID";

        cmd = new OracleCommand(update, con);
        cmd.CommandType = CommandType.Text;

        // TICKETSOURCE, ABOUTLLSID, CATEGORYID, CONTACTEMAIL, TICKETID
        OracleParameter p_TICKETSOURCE =
            new OracleParameter("p_TICKETSOURCE", OracleDbType.NVarchar2, ParameterDirection.Input);
        p_TICKETSOURCE.Size = 20;
        p_TICKETSOURCE.Value = ATicketSource;
        cmd.Parameters.Add(p_TICKETSOURCE);

        OracleParameter p_ABOUTSOMEID =
            new OracleParameter("p_ABOUTSOMEID", OracleDbType.Int32, ParameterDirection.Input);
        p_ABOUTSOMEID.Value = AAboutSOMEID;
        cmd.Parameters.Add(p_ABOUTSOMEID);

        OracleParameter p_CATEGORYID =
            new OracleParameter("p_CATEGORYID", OracleDbType.Int32, ParameterDirection.Input);
        p_CATEGORYID.Value = ACategoryID;
        cmd.Parameters.Add(p_CATEGORYID);

        OracleParameter p_CONTACTEMAIL =
            new OracleParameter("p_CONTACTEMAIL", OracleDbType.NVarchar2, ParameterDirection.Input);
        p_CONTACTEMAIL.Size = 100;
        p_CONTACTEMAIL.Value = AContactsEmail;
        cmd.Parameters.Add(p_CONTACTEMAIL);

        OracleParameter p_TICKETID =
            new OracleParameter("p_TICKETID", OracleDbType.NVarchar2, ParameterDirection.Input);
        p_TICKETID.Size = 20;
        p_TICKETID.Value = ATicketID;
        cmd.Parameters.Add(p_TICKETID);

        using (var transaction = con.BeginTransaction())
        {
            try
            {
                cmd.Transaction = transaction;
                cmd.ExecuteNonQuery();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw;
            }
        }

        MessageBox.Show("Apparent success");
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
    Popul8TheGrid();
}

3 个答案:

答案 0 :(得分:6)

你的try / catch块完全坏了。

你必须先修复它,然后你才能看到真正的错误。

try
{
    ot = con.BeginTransaction();
    cmd.Transaction = ot;
    cmd.ExecuteNonQuery();
    ot.Commit();
    MessageBox.Show("Success"); // <-- this should be here, not after the catch!
}
catch (Exception)
{
    ot.Rollback();
    throw;   // <-- this is important otherwise the exception is swallowed!
}

答案 1 :(得分:5)

我注意到你真的无法区分是否抛出异常并且事务已回滚或事务是否成功。看,你正在捕获异常,回滚事务,然后仍然显示消息框“显然成功”。因为你吞下了这个例外并且堕落了。有一个原因people scream from the top of rooftops to not swallow exceptions

所以,我怀疑是一个异常被抛出,你正在抓住它,回滚,然后变得困惑,因为你摔倒并显示消息框。这是编写得很糟糕的代码,编写错误的代码会引入这样的错误。

至少,如果我是你,我会重新抛出异常。

catch (Exception) {
    ot.Rollback();
    throw;
}

但更好的是,只需将事务的用法包装在using

using(var transaction = con.BeginTransaction()) {
    cmd.Transaction = transaction;
    cmd.ExecuteNonQuery();
    transaction.Commit();
}
  

为什么我的更新语句没有更新表?

您的代码中存在错误。其中一个陈述

ot = con.BeginTransaction();
cmd.Transaction = ot;
cmd.ExecuteNonQuery();
ot.Commit();

正在抛出异常,但你不知道它,因为你吞下了所有异常。停止吞下异常,你会发现这些行中的哪一行正在抛出异常以及原因。然后,您将获得更多信息来调试您的潜在问题。

答案 2 :(得分:3)

此代码在不通知用户的情况下回滚事务。

   catch (Exception)
   {
       ot.Rollback();
   }