'Import คำสั่งที่ใช้งาน
Imports System.Data.OleDb
Imports System.Data
'ประกาศตัวแปร
Public Class Form2
Dim cnn As New OleDbConnection
Dim dr As OleDbDataReader
Dim gSQL As String
'การติดต่อฐานข้อมูล
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strcon As String
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\asiafarm\asiafarm.accdb;"
cnn.ConnectionString = strcon
cnn.Open()
End Sub
'เพิ่มข้อมูลสุกร
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cmd As New OleDbCommand
Try
gSQL = "insert into tbDataPig values ("
gSQL += "'" & TextBox1.Text & "',"
gSQL += "'" & TextBox2.Text & "',"
gSQL += "'" & DateTimePicker1.Text & "',"
gSQL += "'" & TextBox3.Text & "',"
gSQL += "'" & TextBox4.Text & "'"
gSQL += ")"
With cmd
.CommandType = CommandType.Text
.CommandText = gSQL
.Connection = cnn
.ExecuteNonQuery()
End With
MessageBox.Show("เพิ่มข้อมูลสุกรแล้ว")
Catch ex As Exception
MessageBox.Show("คุณกรอกข้อมูลผิดหรือซ้ำ !!!!!! กรุณากรอกข้อมูลใหม่")
End Try
End Sub
' คันหาข้อมูลสุกร
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rdCMD As New OleDbCommand
gSQL = " Select * From tbDataPig where PigId ='" & TextBox5.Text & "'"
With rdCMD
.CommandType = CommandType.Text
.CommandText = gSQL
.Connection = cnn
dr = .ExecuteReader
End With
dr.Read()
If dr.HasRows Then 'พบข้อมูล
TextBox1.Text = dr.Item("PigId").ToString
TextBox2.Text = dr.Item("Sex").ToString
DateTimePicker1.Text = dr.Item("Birthday").ToString
TextBox3.Text = dr.Item("FatherCode").ToString
TextBox4.Text = dr.Item("MotherCode").ToString
Else ' ไม่พบข้อมูล
MessageBox.Show("ไม่พบข้อมูล !!!")
End If
End Sub
End Class