Created
May 2, 2016 14:43
-
-
Save facebookegypt/3dbc65d63104dba22e8d867ca44c62b4 to your computer and use it in GitHub Desktop.
Database Transaction in VB 2010
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Create VB 2010 Project | |
'Database Name (Info) | |
'Create Table name (EMP) with structure (ID Counter PRIMARY KEY, FName Varchar (30)) | |
'Create Windows form (Form1) | |
'Place one TextBox1 on the form which will contain the SQL Commands separated by ; | |
'TOP OF THE FORM | |
Imports System.Data.Oledb | |
Public Class Form1 | |
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load | |
'Connect to Database | |
If CN.State = 1 Then CN.Close() | |
CN.ConnectionString = ("Provider=Microsoft.Jet.OleDB.4.0 ; Data Source=Info.mdb") 'if using access 2003 | |
CN.Open() | |
End If | |
End Sub | |
'Place Button1 on the Form | |
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click | |
'run SQL Command | |
' Start a local transaction with ReadCommitted isolation level. | |
Dim SS As OleDbTransaction = CN.BeginTransaction(IsolationLevel.ReadCommitted) | |
Dim ExCMD As New OleDbCommand | |
Try | |
With ExCMD | |
.Connection = CN | |
.CommandType = CommandType.Text | |
' Assign transaction object for a pending local transaction. | |
.Transaction = SS | |
'Many SQL Commands in the textbox separated by ; | |
.CommandText = TextBox1.Text | |
End With | |
' Commit the transaction. | |
SS.Commit() | |
Catch ex As Exception | |
Try | |
'if something went wrong | |
SS.Rollback() | |
Catch | |
' Do nothing here; transaction is not active. | |
End Try | |
MessageBox.Show(ex.ToString(), "ERROR Please contact Developer", MessageBoxButtons.OK, MessageBoxIcon.Error) | |
End Try | |
objwriter.Close() | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment