ption Explicit On
Option Strict On
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
Dim Conn As SqlConnection
Dim com As New SqlCommand()
Dim dr As SqlDataReader
Dim tr As SqlTransaction
Dim sb As New StringBuilder()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
Dim strConn As String
strConn = "Data Source=KITTIPOOM-NB\SQLEXPRESS;Initial Catalog=Database_HospitalAnimal;Integrated Security=True"
Conn = New SqlConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
tr = Conn.BeginTransaction()
sb = New StringBuilder()
sb.Append("INSERT INTO test(admin)")
sb.Append(" VALUES (@admin)")
Dim sqlSave As String = sb.ToString()
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = Conn
.Transaction = tr
.Parameters.Clear()
.Parameters.Add("@admin", SqlDbType.NVarChar).Value = TextBox1.Text.Trim()
.ExecuteNonQuery()
End With
MessageBox.Show("Inserted")