Imports System.Data.SqlClient
Public Class frmMedicineEdit
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim strsql As String
Dim dt As New DataTable ' แสดงข้อมูลของ dgvMedicine
Dim dtAdapter As SqlDataAdapter
Private Sub frmMedicineEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'con.ConnectionString = "Data Source=localhost; Initial Catalog = Safety; Integrated Security= true"
con.ConnectionString = ("user id = sa;data source = 192.168.5.200 ; persist security info = True;initial catalog = Safety ;password = ttcadmin")
cmd.Connection = con
cmd.Connection = con
con.Open()
End Sub
Sub Medicine()
strsql = " select * From Medicine Order by Med_id ASC "
dtAdapter = New SqlDataAdapter(strsql, con)
dtAdapter.Fill(dt)
frmMedicine.dgvMedicine.DataSource = dt
With frmMedicine.dgvMedicine
.AllowUserToAddRows = False
.BorderStyle = BorderStyle.None
.Columns(0).HeaderText = "รหัสยา"
.Columns(1).HeaderText = "ประเภทยา"
.Columns(2).HeaderText = "ชื่อยา"
.Columns(3).HeaderText = "ราคา/หน่วย"
.Columns(4).HeaderText = "หน่วย"
.Columns(5).HeaderText = "จำนวน"
.Columns(2).Width = "200"
.Columns(3).Width = "80"
.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige 'กำหนดตารางสลับสี
End With
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
strsql = "UPDATE Medicine SET Med_type = '" & (cbxMed_type.Text) & "' , Med_name = '" & (txtMed_Name.Text) & "', Med_unit = '" & (cbxMed_unit.Text) & "' Where Med_id = '" & txtMed_id.Text & "' "
With cmd
.Connection = con
.CommandText = strsql
.CommandType = CommandType.Text
.ExecuteNonQuery()
MessageBox.Show("แก้ไขข้อมูลเสร็จเรียบร้อย", "แก้ไขข้อมูลยา", MessageBoxButtons.OK, MessageBoxIcon.Information)
End With
frmMedicine.dgvMedicine.DataSource.clear()
Medicine()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class
Tag : .NET, Ms SQL Server 2012, VS 2010 (.NET 4.x), Windows