为什么我的更新语句没有更新表
本文关键字:更新 语句 我的 为什么 | 更新日期: 2023-09-27 18:28:13
它进行了编译,似乎在运行,但记录没有更改。
这是我的代码:
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();
}
您的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!
}
我注意到,您真的没有办法区分是否抛出了异常并回滚了事务,或者事务是否成功。看,您正在捕获异常,回滚事务,然后仍然显示消息框"明显成功"。因为您正在吞下异常并失败。人们从屋顶上尖叫不吞下例外是有原因的。
所以,我怀疑有人抛出了一个异常,你抓住了它,向后滚动,然后因为摔倒而感到困惑,并显示了消息框。这是写得不好的代码,写得不差的代码会引入这样的错误。
至少,如果我是你,我会重新考虑这个例外。
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();
正在抛出一个异常,但您不知道,因为您正在吞下所有异常。停止吞下异常,您会发现这些行中的哪一行抛出了异常以及原因。然后,您将有更多的信息来调试您的底层问题。
此代码在不通知用户的情况下回滚事务。
catch (Exception)
{
ot.Rollback();
}