如何在.net c# 中的数据库对象中应用事务回滚
有谁知道如何在.net c# 中的数据库对象中应用事务回滚?
例子: 我最初有以下代码:
protected void btnSave_Click(object sender, EventArgs e)
{
try {
m_test = CreateTest();
} catch{}
}
private Db.Test CreateTest()
{
var test= new Test();
test.Title = sTitle.SelectedValue;
test.FirstName = sFirstName.Text;
test.LastName = sLastName.Text;
test.PhoneHome = sHomePhone.Text;
test.PhoneWork = sWorkPhone.Text;
test.PhoneMobile = sMobile.Text;
test.EmailAddress = sEmail.Text;
test.Save();
return test;
}
然后我尝试将事务回滚操作应用到代码中,并在其中应用以下代码...然后我堆栈在方法 CreateTest 下。我不知道如何应用数据库对象的代码
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection connDB = new SqlConnection();
SqlCommand cmdExecuting = new SqlCommand();
try {
connDB = new SqlConnection(connection_string);
cmdExecuting.Connection = connDB;
connDB.Open();
cmdExecuting.Transaction = connDB.BeginTransaction();
m_test = CreateTest(cmdExecuting);
if (m_test != 0) {
cmdExecuting.Transaction.Rollback();
return;
}
} catch{}
}
private Db.Test CreateTest(SqlCommand cmdExecuting)
{
var test= new Test();
test.Title = sTitle.SelectedValue;
test.FirstName = sFirstName.Text;
test.LastName = sLastName.Text;
test.PhoneHome = sHomePhone.Text;
test.PhoneWork = sWorkPhone.Text;
test.PhoneMobile = sMobile.Text;
test.EmailAddress = sEmail.Text;
test.Save();
return test;
}
有谁知道在这种情况下如何在 CreateTest() 中进行编码?
Does anyone know how to apply transaction roll back in database object in .net c#?
Example:
I have the following code originally:
protected void btnSave_Click(object sender, EventArgs e)
{
try {
m_test = CreateTest();
} catch{}
}
private Db.Test CreateTest()
{
var test= new Test();
test.Title = sTitle.SelectedValue;
test.FirstName = sFirstName.Text;
test.LastName = sLastName.Text;
test.PhoneHome = sHomePhone.Text;
test.PhoneWork = sWorkPhone.Text;
test.PhoneMobile = sMobile.Text;
test.EmailAddress = sEmail.Text;
test.Save();
return test;
}
Then I trying to apply transaction roll back action into the code and applied the following code inside... then I stack under the method CreateTest. I got no idea how could I applied the code for the database object
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection connDB = new SqlConnection();
SqlCommand cmdExecuting = new SqlCommand();
try {
connDB = new SqlConnection(connection_string);
cmdExecuting.Connection = connDB;
connDB.Open();
cmdExecuting.Transaction = connDB.BeginTransaction();
m_test = CreateTest(cmdExecuting);
if (m_test != 0) {
cmdExecuting.Transaction.Rollback();
return;
}
} catch{}
}
private Db.Test CreateTest(SqlCommand cmdExecuting)
{
var test= new Test();
test.Title = sTitle.SelectedValue;
test.FirstName = sFirstName.Text;
test.LastName = sLastName.Text;
test.PhoneHome = sHomePhone.Text;
test.PhoneWork = sWorkPhone.Text;
test.PhoneMobile = sMobile.Text;
test.EmailAddress = sEmail.Text;
test.Save();
return test;
}
Does anyone know how can I code in the CreateTest() in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下 TransactionScope 类,它提供了几个好处包括
范围内建立的连接
当异常发生时
更清晰一些,因为它减少了
所需的代码量
。
另请参阅文章在 .NET Framework 2.0 中介绍 System.Transactions一个很好的起点。
Take a look at the TransactionScope class, which provides several benefits including
connections made within scope
when exceptions occur
a bit more legible since it reduces
the amount of code needed
.
Also, see the article Introducing System.Transactions in the .NET Framework 2.0 for a great starting point.
您可以采取以下措施,您不需要在后端处理事务,sqltransaction 对象将处理它。您可以将此代码放在您自己的事件中,无论哪个事件负责将数据插入数据库
you can take help of the following you dont need to take care of transaction at backend sqltransaction object will take care of it. you can put this code in your own event whichever is responsible for inserting the data in the database