Buka project Koneksi String VB.NET 2010 From App.Config dan buat form seperti gambar di bawah ini
Ketikkan kode berikut :
Imports System.Data.SqlClient Imports System.Configuration Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connectionString As String Dim cnn As SqlConnection Dim cmd As SqlCommand Dim sql As String Dim transaction As SqlTransaction connectionString = ConfigurationManager.AppSettings("ConString").ToString() cnn = New SqlConnection(connectionString) sql = "select COUNT(*) as JumlahItem from Barang" Try cnn.Open() transaction = cnn.BeginTransaction cmd = New SqlCommand(sql, cnn) cmd.Transaction = transaction Dim count As Int32 = Convert.ToInt32(cmd.ExecuteScalar()) transaction.Commit() 'transaction.Rollback() cmd.Dispose() cnn.Close() MsgBox(" No. of Rows " & count) Catch ex As Exception MsgBox("Gagal.") End Try End Sub End ClassCatatan :
Menggunakan metode Komit atau Rollback akan membantu dalam proses penyimpanan transaksi didalam database agar tidak terjadi redundance/atau data bentrok. Bila keluaran dari proses query sesuai maka metode Komit dijalankan (meneruskan eksekusi database), sebaliknya metode RollBack akan dijalankan (membatalkan eksekusi database).
transaction.Commit() 'transaction.Rollback()untuk fungsi transaction.Rollback() penulis kasih tanda komentar, untuk menggunakannya silahkan hapus komentarnya
0 komentar:
Posting Komentar