ช่วยทีคับผู้รู้ Addข้อมุลจากVbลงSQL server ไม่ได้คับ
โค๊ต Code (VB.NET)
Option Explicit On
Option Strict On
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim Conn As SqlConnection
Dim com As SqlCommand
Dim tr As SqlTransaction
Dim sb As StringBuilder
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If Conn IsNot Nothing Then
Conn.Close()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
strConn = DBConnString.strConn
Conn = New SqlConnection
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If MessageBox.Show("คุณต้องการเพิ่มใหม่ใช่หรือไม่ !!!", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
tr = Conn.BeginTransaction()
sb = New StringBuilder()
sb.Append("INSERT INTO Customer (CustomerID,CustomerName,CustomerLastname,address,postal,Phone,Mail,TypeCar,Brand,Model,Licenseplate,Color,NumberEngine,NumberBody)")
sb.Append(" VALUES (@CustomerID,@CustomerName,@CustomerLastname,@address,@postal,@Phone,@Mail,@TypeCar,@Brand,@Model,@Licenseplate,@Color,@NumberEngine,@NumberBody)")
Dim sqlAdd As String
sqlAdd = sb.ToString()
With com
.CommandText = sqlAdd
.CommandType = CommandType.Text
.Connection = Conn
.Transaction = tr
.Parameters.Clear()
.Parameters.Add("@CustomerID", SqlDbType.Char).Value = TextBox1.Text.Trim
.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = TextBox2.Text.Trim
.Parameters.Add("@CustomerLastname", SqlDbType.NVarChar).Value = TextBox3.Text.Trim
.Parameters.Add("@address", SqlDbType.NVarChar).Value = TextBox4.Text.Trim
.Parameters.Add("@postal", SqlDbType.Char).Value = TextBox5.Text.Trim
.Parameters.Add("@Phone", SqlDbType.Char).Value = TextBox6.Text.Trim
.Parameters.Add("@Mail", SqlDbType.NVarChar).Value = TextBox7.Text.Trim
.Parameters.Add("@TypeCar", SqlDbType.NVarChar).Value = TextBox8.Text.Trim
.Parameters.Add("@Brand", SqlDbType.NVarChar).Value = TextBox9.Text.Trim
.Parameters.Add("@Model", SqlDbType.NVarChar).Value = TextBox10.Text.Trim
.Parameters.Add("@Licenseplate", SqlDbType.NVarChar).Value = TextBox11.Text.Trim
.Parameters.Add("@Color", SqlDbType.NVarChar).Value = TextBox12.Text.Trim
.Parameters.Add("@NumberEngine", SqlDbType.NVarChar).Value = TextBox13.Text.Trim
.Parameters.Add("@NumberBody", SqlDbType.NVarChar).Value = TextBox14.Text.Trim
.ExecuteNonQuery()
End With
tr.Commit()
MessageBox.Show("เพิ่มใหม่เรียบร้อย", "คำยืนยัน", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
TextBox1.Focus()
End Sub
End Class
Tag : Ms SQL Server 2005, Win (Windows App), VS 2005 (.NET 2.x)