1> transactions at the store procedure level.
CREATE PROCEDURE Addinfo
(@studentname varchar (),....)
As
BEGIN TRANSACTION
Insert ...
Insert ....
If .....
ROLLBACK TRANSACTION
Update ...
.....
Commit TRANSACTION
Note: You can use Save transaction to select the location of the rollback in the stored procedure
2> transaction processing at the database level.
Imports System.Data.SqlClient namespaces need to be imported.
' This function would add student ' s infomation and its parent ' s information concurrently!
' So we should use transaction!
Public Shared Function Insertinfo (ByVal student as Clsstudent, ByVal parent as clsparent) as Boolean
Dim success as Boolean = True
Dim cmdstudent as New SqlCommand (insert into student (Name,sex,classname) VALUES (@name, @sex, @classname) ", CNN)
Dim cmdparent as New SqlCommand (insert into parent (name,sex,salary) VALUES (@name, @sex, @salary) ", CNN)
Dim Cmdgetstudentid as New SqlCommand ("Select StudentID from student where Name= @name", CNN)
Dim Cmdgetparentid as New SqlCommand ("Select ParentID from parent where Name= @name", CNN)
Dim cmdstudentparent as New SqlCommand ("INSERT into studentparent (Studentid,parentid) VALUES (@studentid, @parentid)", CNN
Catch ex as Exception
Transaction. Rollback ()
Success = False
MessageBox.Show (ex. Message)
Finally
Cnn. Close ()
End Try
Return success
End Function
3> transaction processing at the page level, also known as COM-level transactions.
Imports System.Data.Sqlclient and Imports System.EnterpriseServices required
' This function would add student ' s infomation and its parent ' s information concurrently!
' So we should use transaction!
Public Shared Function Insertinfo (ByVal student as Clsstudent, ByVal parent as clsparent) as Boolean
Dim success as Boolean = True
Dim cmdstudent as New SqlCommand (insert into student (Name,sex,classname) VALUES (@name, @sex, @classname) ", CNN)
Dim cmdparent as New SqlCommand (insert into parent (name,sex,salary) VALUES (@name, @sex, @salary) ", CNN)
Dim Cmdgetstudentid as New SqlCommand ("Select StudentID from student where Name= @name", CNN)
Dim Cmdgetparentid as New SqlCommand ("Select ParentID from parent where Name= @name", CNN)
Dim cmdstudentparent as New SqlCommand ("INSERT into studentparent (Studentid,parentid) VALUES (@studentid, @parentid)", CNN
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.