Private Sub ButtonSave_Click() Handles ButtonSave.Click
If IsDataComplete() = False Then
Exit Sub
End If
sql = "INSERT INTO Employee "
If Not (PictureBox1.Image Is Nothing) Then
sql &= "(Emp_ID,Emp_Name,Emp_Las,Emp_Age,Emp_Num,Emp_Add,Picture)" &
"VALUES(@id,@name,@last,@age,@num,@add,@pic)"
Else
sql &= "(Emp_ID,Emp_Name,Emp_Las,Emp_Age,Emp_Num,Emp_Add)" &
"VALUES(@id,@name,@last,@age,@num,@add)"
End If
Dim command As New SqlCommand(sql, conection)
command.Parameters.Clear()
command.CommandText = sql
command.Parameters.AddWithValue("@id", TextID.Text)
command.Parameters.AddWithValue("@name", Textname.Text)
command.Parameters.AddWithValue("@last", Textlats.Text)
command.Parameters.AddWithValue("@age", Textage.Text)
command.Parameters.AddWithValue("@num", Textnum.Text)
command.Parameters.AddWithValue("@add", Textadd.Text)
If Not (PictureBox1.Image Is Nothing) Then
Dim pic() As Byte = ReadImage()
command.Parameters.AddWithValue("pic", pic)
End If
Dim result As Integer = command.ExecuteNonQuery()
If result = -1 Then
MessageBox.Show("เกิดข้อผิดพลาดไม่สามารถเพิ่มข้อมูลได้")
Else
MessageBox.Show("บันทึกข้อมูลแล้ว")
ButtonAdd.PerformClick()
End If
End Sub
'อันนี้คือตัวข้อมูลที่จะอัพเดท คับ
Code (VB.NET)
[vb] Private Sub Btnupdate_Click() Handles Btnupdate.Click
If TextID.Text = "" Or IsDataComplete() = False Then
Exit Sub
End If
Dim s As String
If Not (PictureBox1.Image Is Nothing) Then
s = ",Picture=@pic"
Else
s = ""
End If
sql = "UPDATE Employee SET" &
" Emp_ID = @id, Emp_Name = @name,Emp_Las = @last,Emp_Age = @age,Emp_Num = @num,Emp_Add = @add" & s
command.Parameters.Clear()
command.CommandText = sql
command.Parameters.AddWithValue("@id", TextID.Text)
command.Parameters.AddWithValue("@name", Textname.Text)
command.Parameters.AddWithValue("@last", Textlats.Text)
command.Parameters.AddWithValue("@age", Textage.Text)
command.Parameters.AddWithValue("@num", Textnum.Text)
command.Parameters.AddWithValue("@add", Textadd.Text)
If Not (PictureBox1.Image Is Nothing) Then
Dim byteArr() As Byte = ReadImage()
command.Parameters.AddWithValue("pic", byteArr)
End If
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("บันทึกการเปลี่ยนแปลงแล้ว")
UpdateBinding()
Else
MessageBox.Show("เกิดข้อผิดพลาด")
End If
End Sub
End Class
' อันดีเป็นตัวอัพเดทคับ รบกวนช่วยดูหน่อยคับ