Tag : MySQL, VBScript, Win (Windows App), VS 2005 (.NET 2.x)
Date :
2011-07-22 22:30:24
By :
deer
View :
983
Reply :
3
No. 1
Guest
ช่วยที่นะคับ error ตรงนี้แหละคับ
Date :
2011-07-22 22:31:13
By :
deer
No. 2
Guest
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,address,postal)")
sb.Append(" VALUES (@CustomerID,@CustomerName,@address,@postal)")
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("@address", SqlDbType.NVarChar).Value = TextBox4.Text.Trim
.Parameters.Add("@postal", SqlDbType.Char).Value = TextBox5.Text.Trim
.ExecuteNonQuery()
End With
tr.Commit()
MessageBox.Show("เพิ่มใหม่เรียบร้อย", "คำยืนยัน", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
TextBox1.Focus()
End Sub
End Class