如何回滚序列号 [英] How to Rollback a sequence number
本文介绍了如何回滚序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static void AddNew(string stu_name, string address, string Phone, datetime date)
{
String sql = "Select I_Student_ID.NEXTVAL from dual";
Int newStudent_ID = 0;
OracleDatabase db = (OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
using (System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql))
{
try
{
newStudent_ID = Convert.ToInt32(db.ExecuteScalar(cmd));
}
catch (Exception e)
{
cmd.Transation.Rollback();
}
OracleDatabase db = (OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("AddNewStudent"))
{
db.AddInParameter(cmd, "I_Student_ID",DbType.Int32,newStudent_ID);
db.AddInParameter(cmd, "I_STU_Name", DbType.String,stu_name);
db.AddInParameter(cmd, "I_Address", DbType.String,address);
db.AddInParameter(cmd, "I_Phone", DbType.String,Phone);
db.AddInParameter(cmd, "I_Date", DbType.date,date);
db.ExecuteNonQuery(cmd);
}
}
}
如果转换有问题,我想回滚Student_Id的序列号.我不确定如何回滚转换,因为上面的代码不起作用,我无法回滚.Nextval.
谢谢
I want to Rollback the sequence number for Student_Id if the transation has some problems. I am not sure how can I rollback the transation because the above code is not working, I can''t rollback the .Nextval.
Thanks
推荐答案
我的工作:
What I do:
public static DataTable FillDataTable(String StoredProcedure, List<sqlparameter> sqlParameters, String ConnectionName)
{
DataTable tabel = new DataTable("GeneralTable");
SqlTransaction transaction = null;
using (SqlCommand cmd = BaseDatabaseData(StoredProcedure, sqlParameters, ConnectionName))
{
try
{
using (transaction = cmd.Connection.BeginTransaction("SampleTransaction"))
{
cmd.Transaction = transaction;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(tabel);
transaction.Commit();
}
}
catch (Exception ex)
{
String Message = ex.Message;
transaction.Rollback();
throw new Exception(ex.Message, ex.InnerException);
}
finally
{
if (cmd.Connection.State.Equals(ConnectionState.Open))
{
cmd.Connection.Close();
cmd.Connection.Dispose();
}
}
}
return tabel;
}
</sqlparameter>
我知道我使用MSSql连接而不是Oracle连接.我相信设置事务将与Oracle连接以及mssql连接一起使用.
方法BaseDatabaseData向我返回了一个打开的SqlConnection
I know I use the MSSql connection and not the Oracle one. I believe that setting up a transaction will work with the Oracle connection as with the mssql connection.
Method BaseDatabaseData returns me an open SqlConnection
这篇关于如何回滚序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文