sqlserver中的回滚 [英] rollback in sqlserver

查看:320
本文介绍了sqlserver中的回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有两个Web表单和两个不同的表,用于保存这两个Web表单的数据,
在第一个Web表单上,我有一个超链接,用于从第一页重定向到第二页.现在,当我单击超链接页面时,我将第一页的数据保存在第一张表中.现在,当我单击第二页的保存时,如果第二页的插入失败,我想回滚第一次插入的内容,即在第一页上进行的
为了插入,我简单地做了一个存储过程.

如何回滚插入..

在此先感谢...

Hi All,

I have two web forms and two different tables for save data of these two web forms,
on first web form i have a hyperlink which is used to redirect to second page from first page . Now when i click on hyperlink page i save the data of first page in first table now when i click on save of second page if second page insertion failed i want to rollback first insertion i.e., made on first page
for insertion i simple made a stored procedure.

how to rollback insertion..

Thanks in Advance...

推荐答案

我可以想到两种方法:

1.在应用程序中执行事务管理.创建一个连接并以form1开始事务.现在,在form2中使用相同的连接对象.如果在form2中保存效果良好,请执行一次提交.否则进行回滚.请务必注意关闭和放置连接对象.如果打开了第1页中的连接并进行了保存.然后转到第2页,仅关闭窗口.您的连接必须关闭并且事务必须回滚.

2.您可以在存储过程中进行事务处理.在应用程序中,当用户从page1移至page2时,只需将数据保留在Session中即可.当用户单击保存时,将所有数据保存在一个数据库命中中.这是您的应用程序的工作方式:

第1页->第2页:在会话中保存数据
Page2.Save:获取Page1和Page2数据并将其终止到DB.
在您的存储过程中:

1.主要SP称为
2.开始互动
3.执行SP保存第1页数据
4.执行SP来保存Page2数据
5.提交或回滚
6.将输出返回到page2
There are two ways I can think of:

1. Do Transaction management in the application. Create a connection and begin transaction in the form1. Now use the same connection object in the form2. If the save in form2 works well, do a commit. Or else do a rollback. Be sure to take care of closing and disposing connection object. If one opens the connection in page 1 and does the save. Then goes to page 2 and just closes the window. Your connection must get closed and transaction must get rolled back.

2. You can do transaction in your stored procedure. In the application, when user moves from page1 to page2, just keep the data in the Session. When the user clicks on save, save all the data in one DB hit. Here is how your application would work:

Page1 -> Page2: Save data in the Session
Page2.Save: Get Page1 and Page2 data and end it to DB.
In your stored procedure:

1. Main SP called
2. Begin tansaction
3. Execute SP to save Page1 data
4. Execute SP to save Page2 data
5. Commit or rollback
6. Return output to page2




如果我正确理解了您的问题,则您试图分两部分进行事务,在两者之间进行用户对话.永远不要这样做.

当事务开始时,您只需进行必要的修改,检查错误等,最后进行提交或回滚.中间有任何类型的UI操作都将导致锁定相关的问题等.

因此,我的建议是,当用户切换到第二页时,将必要的数据存储在其他位置(会话等),并且当用户最终按下保存时,对所有数据进行必要的有效性检查并将数据保存在一个简短的事务中.
Hi,

If I understood your problem correctly, you''re trying to do the transaction in two parts, having a user conversation in between. This should never be done.

When a transaction starts, you simply make the necessary modifications, check for errors etc and in the end, commit or rollback. Having any kind of UI operations in the middle will cause locking related problems etc.

So my advice is that when the user changes to second page, store the necessary data somewhere else (session etc) and when the user finally presses the save, make necessary validity checks against all data and save the data in a single, short, transaction.


这篇关于sqlserver中的回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆