dim conn as new sqlconnection(constr)
conn.open()
sql as string = "SELECT * FROM Employee"
dim cmd as new sqlcommand(sql,conn)
dim adapter as new sqldataadapter(cmd)
dim data as dataset()
adapter.fill(data,"E")
Datagridview1.datasource =data.tables("E")
conn.close
conn = new sqlconnection(constr)
conn.open
sql as string = "INSERT INTO Employee VALUES ('test','test') ก็ว่ากันไป
dim cmd as new sqlcommand(sql)
dim reader as sqldatareader = cmd.ExecuteReader
conn.close
LoadView()
คำสั่งข้างบนก็คือ แทรกข้อมูลลงในตารางEmployee แล้วเมื่อเสร้จให้เรียกโหลด Form Loadview() ขึ้นมา
โดยForm Loadview จะสร้างขึ้นมาใน class นั่นๆ หรือ Module ก็ได้โดยเอาคำสั่งเหมือนกับ Form_Load ได้เลย
เช่น
Code (VB.NET)
Private sub Loadview()
dim conn as new sqlconnection(constr)
conn.open()
sql as string = "SELECT * FROM Employee"
dim cmd as new sqlcommand(sql,conn)
dim adapter as new sqldataadapter(cmd)
dim data as dataset()
adapter.fill(data,"E")
Datagridview1.datasource =data.tables("E")
conn.close
end sub
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class ตั้งค่าเบื้องต้นระบบ_ข้อมูลบุคลากร
Private Sub ออกจากระบบToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ออกจากระบบToolStripMenuItem.Click
Me.Hide()
เข้าสู่ระบบ.Show()
End Sub
Private Sub ขอมลลกคาToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลลกคาToolStripMenuItem.Click
Me.Hide()
ตั้งค่าเบื้องต้นระบบ_ข้อมูลลูกค้า.Show()
End Sub
Private Sub ขอมลบคลากรToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลบคลากรToolStripMenuItem.Click
End Sub
Private Sub ขอมลเงนเดอนToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลเงนเดอนToolStripMenuItem.Click
Me.Hide()
ตั้งค่าเบื้องต้นระบบ_ข้อมูลอัตราเงินเดือน.Show()
End Sub
Private Sub ขอมลอตราคาบรการToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลอตราคาบรการToolStripMenuItem.Click
Me.Hide()
ตั้งค่าเบื้องต้นระบบ_ข้อมูลอัตราค่าบริการ.Show()
End Sub
Private Sub ขอมลอปกรณToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลอปกรณToolStripMenuItem.Click
Me.Hide()
ตั้งค่าเบื้องต้นระบบ_ข้อมูลอุปกรณ์.Show()
End Sub
Private Sub ลกคาToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ลกคาToolStripMenuItem.Click
Me.Hide()
การคิดเงินเดือน.Show()
End Sub
Private Sub การเบกใชอปกรณToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles การเบกใชอปกรณToolStripMenuItem.Click
Me.Hide()
การเบิกใช้อุปกรณ์.Show()
End Sub
Private Sub การรบรถToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles การรบรถToolStripMenuItem.Click
Me.Hide()
การให้บริการรับรถคืนรถ_การรับรถ.Show()
End Sub
Private Sub การคดคาบรการToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles การคดคาบรการToolStripMenuItem.Click
Me.Hide()
การให้บริการรับรถคืนรถ_การคิดค่าบริการ.Show()
End Sub
Private Sub การคนรถToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles การคนรถToolStripMenuItem1.Click
Me.Hide()
การให้บริการรับรถคืนรถ_การคืนรถ.Show()
End Sub
Private Sub รปแบบเอกสารToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles รปแบบเอกสารToolStripMenuItem.Click
Me.Hide()
พิมพ์รายงานรูปแบบเอกสาร.Show()
End Sub
Private Sub รปแบบกราฟToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles รปแบบกราฟToolStripMenuItem.Click
Me.Hide()
พิมพ์รายงานรูปแบบกราฟ.Show()
End Sub
Public Sub loadfile()
Dim sqlQuery As String = "select * from personnel"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCmd As New MySqlCommand
Dim sqlTable As New DataTable
Dim i As Integer
With sqlCmd
.CommandText = sqlQuery
.Connection = con
End With
With sqlAdapter
.SelectCommand = sqlCmd
.Fill(sqlTable)
End With
For i = 0 To sqlTable.Rows.Count - 1
DataGridView1.Rows.Add(sqlTable.Rows(i)("per_no"), (sqlTable.Rows(i)("per_name")), (sqlTable.Rows(i)("per_sname")), (sqlTable.Rows(i)("per_tel")), (sqlTable.Rows(i)("per_address")), (sqlTable.Rows(i)("per_id")), (sqlTable.Rows(i)("sal_rate_position")))
Next
End Sub
Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
If (TextBox5.Text = "") Then
Return
End If
If (Not IsNumeric(TextBox5.Text)) Then
MsgBox("ใส่ตัวเลขเท่านั้น กรุณาใส่ใหม่")
TextBox5.Clear()
TextBox5.Focus()
End If
End Sub
Private Sub ตั้งค่าเบื้องต้นระบบ_ข้อมูลบุคลากร_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If con.State = ConnectionState.Closed Then
con.Open()
End If
loadfile()
con.Close()
End Sub
Private Sub เพิ่ม_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles เพิ่ม.Click
Dim Query As String
Query = "INSERT INTO personnel"
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=carcare;User ID=root;Password=root;")
Dim sql As MySqlCommand = New MySqlCommand(Query, con)
Query = "INSERT INTO personnel(per_no,per_name,per_sname,per_tel,per_address,per_id,sal_rate_position)VALUES("
Query = Query + TextBox2.Text + ",'" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox7.Text + "','" + TextBox6.Text + "','" + TextBox5.Text + "','" + TextBox8.Text + "')"
con.Open()
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
'lblMsg.Text = "Record is Successfully Inserted"
'Else
'lblMsg.Text = "Record is not Inserted"
'End If
con.Close()
End Sub
Private Sub ลบ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ลบ.Click
Dim Query As String
con.Open()
Dim xx As Integer
xx = DataGridView1.CurrentRow.Index
'MsgBox(DataGridView1.Item(0, xx).Value)
Dim zz As String
zz = DataGridView1.Item(0, xx).Value.ToString
Query = "Delete FROM personnel WHERE per_no =" + zz
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
MsgBox(Query)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
' lblMsg.Text = "Record is Successfully Deleted"
'Else
' lblMsg.Text = "Record is not Deleted"
'End If
con.Close()
End Sub
Private Sub แก้ไข_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles แก้ไข.Click
Dim Query As String
con.Open()
Query = "UPDATE personnel SET per_name ='" + TextBox3.Text + "'"
Query = Query + ",per_sname = '" + TextBox4.Text + "'"
Query = Query + ",per_tel = '" + TextBox7.Text + "'"
Query = Query + ",per_address = '" + TextBox6.Text + "'"
Query = Query + ",per_id = '" + TextBox5.Text + "'"
Query = Query + ",sal_rate_position = '" + TextBox8.Text + "'"
Query = Query + " WHERE per_no = '" + TextBox2.Text + "'"
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
MsgBox(Query)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
'lblMsg.Text = "Record is Successfully Updated"
' lblMsg.Text = "Record is not Updated"
'End If
con.Close()
End Sub
Private Sub ยกเลิก_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ยกเลิก.Click
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox7.Text = ""
TextBox6.Text = ""
TextBox5.Text = ""
TextBox8.Text = ""
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
TextBox2.Text = DataGridView1.Item(0, i).Value
TextBox3.Text = DataGridView1.Item(1, i).Value
TextBox4.Text = DataGridView1.Item(2, i).Value
TextBox7.Text = DataGridView1.Item(3, i).Value
TextBox6.Text = DataGridView1.Item(4, i).Value
TextBox5.Text = DataGridView1.Item(5, i).Value
TextBox8.Text = DataGridView1.Item(6, i).Value
End Sub
End Class
For i = 0 To sqlTable.Rows.Count - 1
DataGridView1.Rows.Add(sqlTable.Rows(i)("per_no"), (sqlTable.Rows(i)("per_name")), (sqlTable.Rows(i)("per_sname")), (sqlTable.Rows(i)("per_tel")), (sqlTable.Rows(i)("per_address")), (sqlTable.Rows(i)("per_id")), (sqlTable.Rows(i)("sal_rate_position")))
Next
End Sub
Private Sub เพิ่ม_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles เพิ่ม.Click
Dim Query As String
Query = "INSERT INTO personnel"
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=carcare;User ID=root;Password=root;")
Dim sql As MySqlCommand = New MySqlCommand(Query, con)
Query = "INSERT INTO personnel(per_no,per_name,per_sname,per_tel,per_address,per_id,sal_rate_position)VALUES("
Query = Query + TextBox2.Text + ",'" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox7.Text + "','" + TextBox6.Text + "','" + TextBox5.Text + "','" + TextBox8.Text + "')"
con.Open()
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
'lblMsg.Text = "Record is Successfully Inserted"
'Else
'lblMsg.Text = "Record is not Inserted"
'End If
con.Close()
loadfile()
End Sub
แก้ไข
Code (VB.NET)
Private Sub แก้ไข_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles แก้ไข.Click
Dim Query As String
con.Open()
Query = "UPDATE personnel SET per_name ='" + TextBox3.Text + "'"
Query = Query + ",per_sname = '" + TextBox4.Text + "'"
Query = Query + ",per_tel = '" + TextBox7.Text + "'"
Query = Query + ",per_address = '" + TextBox6.Text + "'"
Query = Query + ",per_id = '" + TextBox5.Text + "'"
Query = Query + ",sal_rate_position = '" + TextBox8.Text + "'"
Query = Query + " WHERE per_no = '" + TextBox2.Text + "'"
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
MsgBox(Query)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
'lblMsg.Text = "Record is Successfully Updated"
' lblMsg.Text = "Record is not Updated"
'End If
con.Close()
loadfile()
End Sub
ลบ
Code (VB.NET)
Private Sub ลบ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ลบ.Click
Dim Query As String
con.Open()
Dim xx As Integer
xx = DataGridView1.CurrentRow.Index
'MsgBox(DataGridView1.Item(0, xx).Value)
Dim zz As String
zz = DataGridView1.Item(0, xx).Value.ToString
Query = "Delete FROM personnel WHERE per_no =" + zz
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
MsgBox(Query)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
' lblMsg.Text = "Record is Successfully Deleted"
'Else
' lblMsg.Text = "Record is not Deleted"
'End If
con.Close()
loadfile()
End Sub
For i = 0 To sqlTable.Rows.Count - 1
DataGridView1.Rows.Add(sqlTable.Rows(i)("per_no"), (sqlTable.Rows(i)("per_name")), (sqlTable.Rows(i)("per_sname")), (sqlTable.Rows(i)("per_tel")), (sqlTable.Rows(i)("per_address")), (sqlTable.Rows(i)("per_id")), (sqlTable.Rows(i)("sal_rate_position")))
Next
End Sub