我的 C# 设备应用程序中有程序。这是它的外观:
private void stk_crane_start_movement()
{
conn.Open();
OracleCommand cmd = new OracleCommand();
OracleTransaction trans;
trans = conn.BeginTransaction();
cmd.Transaction = trans;
cmd.Connection = conn;
conn.AutoCommit = false;
cmd.CommandTimeout = 0;
cmd.CommandText = "dc.stk_crane_start_movement";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("pn_crane_opr_id_no", OracleDbType.Number).Value = empid.ToString();
cmd.Parameters.Add("pn_crane_movement_id_no", OracleDbType.Number).Value = pn_crane_movement_id_no.ToString();
cmd.Parameters.Add(new OracleParameter("pv_error", OracleDbType.VarChar));
cmd.Parameters["pv_error"].Direction = ParameterDirection.Output;
string pv_error;
cmd.ExecuteNonQuery();
pv_error = cmd.Parameters["pv_error"].Value.ToString();
if (pv_error.ToString() == "")
{
trans.Commit();
trans.Dispose();
conn.Close();
cmd.Dispose();
}
else
{
trans.Rollback();
MessageBox.Show("" + pv_error, "Error");
}
}
我ORA-01453: SET TRANSACTION must be first statement of transaction
开始 trans = conn.BeginTransaction();
有人可以向我解释我到底做错了什么吗?
我也试过这样:
private void stk_crane_start_movement()
{
conn.Open();
OracleCommand cmd = conn.CreateCommand();
OracleTransaction trans;
trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = trans;
try
{
cmd.CommandText = "dc.stk_crane_start_movement";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("pn_crane_opr_id_no", OracleDbType.Number).Value = empid.ToString();
cmd.Parameters.Add("pn_crane_movement_id_no", OracleDbType.Number).Value = pn_crane_movement_id_no.ToString();
cmd.Parameters.Add(new OracleParameter("pv_error", OracleDbType.VarChar));
cmd.Parameters["pv_error"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
trans.Commit();
}
catch
{
pv_error = cmd.Parameters["pv_error"].Value.ToString();
MessageBox.Show("" + pv_error, "Error");
try
{
trans.Rollback();
}
catch (OracleException ex)
{
MessageBox.Show("Rollback failed" + ex, "Exception Error");
}
}
}
但是因为它不是我期望的异常错误,所以它不会通过 try catch 语句。我希望它在我的 pv_error 变量被填充时回滚。这就是我在第一个示例中包含 if 语句的原因。
此外,在此之前我没有任何其他交易..