Imports System
Imports System.Text
Imports System.Data
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Windows.Forms
Module CodeProgram
Public DA As SqlDataAdapter
Public DS As DataSet
Private CMB As SqlCommandBuilder
Public SB As StringBuilder
Public TR As SqlTransaction
Private DR As DataRow
Dim Mycon As SqlConnection
Dim Mycmd As SqlCommand
Dim MyReader As SqlDataReader
Dim StrconString, StrSQL As String
Public Sub ConnectDB()
Dim StrconString As String = ("Data Source=ULTIMATEPEE-PC\SQLEXPRESS;Initial Catalog=MyWarehouse;Integrated Security=True")
Mycon = New SqlConnection(StrconString)
If Mycon.State = ConnectionState.Open Then
Mycon.Close()
Else
Mycon.Open()
End If
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
ConnectDB()
TR = Mycon.BeginTransaction()
Try
SB.Remove(0, SB.Length)
SB.Append("INSERT INTO TbCustomer(CustomerID,CustomerName,CustomerAddress,CustomerTel,CustomerEmail)")
SB.Append("VALUES(@CustomerID,@CustomerName,@CustomerAddress,@CustomerTel,@CustomerEmail)")
Dim CMinsert As String
CMinsert = SB.ToString
Mycmd.CommandText = CMinsert
Mycmd.CommandType = CommandType.Text
Mycmd.Connection = Mycon
Mycmd.Transaction = TR
Mycmd.Parameters.Clear()
Mycmd.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = txtID.Text.Trim()
Mycmd.Parameters.Add("@CustomerName", SqlDbType.VarChar).Value = txtName.Text.Trim()
Mycmd.Parameters.Add("@CustomerAddress", SqlDbType.VarChar).Value = txtAddress.Text.Trim()
Mycmd.Parameters.Add("@CustomerTel", SqlDbType.VarChar).Value = txtTel.Text.Trim()
Mycmd.Parameters.Add("@CustomerEmail", SqlDbType.VarChar).Value = txtEmail.Text.Trim()
Mycmd.ExecuteNonQuery()
TR.Commit()
MessageBox.Show("สมัครสมาชิกเรียบร้อยแล้ว", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Clear()
Catch ex As Exception
MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " + ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
TR.Rollback()
End Try
End Sub
Tag : ASP.NET, Ms SQL Server 2008, VB.NET, VS 2005 (.NET 2.x), VS 2008 (.NET 3.x), VS 2010 (.NET 4.x)