imports System.data
imports System.data.oleDb
'กรณีไม่ได้ใส่ pw DB
Dim conStr as string = "Provider=Microsoft.Ace.OleDb.12.0; Data Source= ไฟล์ฐานข้อมูล"
Dim conn as new OleDbConnection (conStr)
conn.Open()
Dim sql as string = "SELECT * FROM ตาราง"
Dim cmd as new OleDbCommand(sql, conn)
.................
.................
conn.close()
Imports System.Data.OleDb
Public Class add
Dim conad As New OleDbConnection
Dim mycommandad As OleDbCommand
Dim sqlad As String
Dim drad As OleDbDataReader
Dim dtad As DataTable
Private Sub add_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conad.Close()
conad.ConnectionString = "Provider=Microsoft.ace.OLEDB.12.0;Data Source = test.accdb"
conad.Open()
showdataad()
sqlad = "select * from product"
mycommandad = New OleDbCommand(sqlad, conad)
drad = mycommandad.ExecuteReader
If drad.HasRows Then
dtad = New DataTable()
dtad.Load(drad)
ComboBox1.DataSource = dtad
ComboBox1.DisplayMember = "p_size"
ComboBox1.ValueMember = "p_id"
Else
MsgBox("ไมีมีฐานข้อมูล", MsgBoxStyle.Question, "ระบบเพิ่มสินค้า")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("กรูณาใส่จำนวนเสาปูนที่ต้องการเพิ่ม", MsgBoxStyle.Critical, "ระบบเพิ่มสินค้า")
Else
Dim sumad As Integer
sumad = 0
sqlad = "select * from product where p_id = '" & ComboBox1.SelectedValue.ToString() & "'"
mycommandad = New OleDbCommand(sqlad, conad)
drad = mycommandad.ExecuteReader
If drad.HasRows Then
drad.Read()
sumad = Val(drad("p_sum"))
End If
sumad = sumad + Val(TextBox1.Text)
sqlad = "UPDATE product SET"
sqlad &= " p_sum = " & sumad & ""
sqlad &= " WHERE (p_id = '" & ComboBox1.SelectedValue.ToString() & "')"
Try
mycommandad = New OleDbCommand(sqlad, conad)
mycommandad.ExecuteNonQuery()
MsgBox("ระบบทำการบันทึกเรียบร้อยแล้ว", MsgBoxStyle.Information, "ระบบเพิ่มสินค้า")
ComboBox1.SelectedIndex = 0
TextBox1.Clear()
showdataad()
Catch ex As Exception
MsgBox("ข้อมูลผิดพลาด", MsgBoxStyle.Question, "ระบบเพิ่มสินค้า")
End Try
End If
End Sub
Private Sub showdataad()
sqlad = "select * from product"
mycommandad = New OleDbCommand(sqlad, conad)
drad = mycommandad.ExecuteReader
If drad.HasRows Then
dtad = New DataTable()
dtad.Load(drad)
DataGridView1.DataSource = dtad
With DataGridView1
If .RowCount > 0 Then
.Columns(0).HeaderText = "ลำดับ"
.Columns(1).HeaderText = "ขนาดเสาปูน"
.Columns(2).HeaderText = "จำนวนที่เหลือ"
.Columns(0).ReadOnly = True
.Columns(1).ReadOnly = True
.Columns(2).ReadOnly = True
.Columns(0).Width = 50
.Columns(1).Width = 120
.Columns(2).Width = 100
End If
End With
Else
MsgBox("ไมีมีฐานข้อมูล", MsgBoxStyle.Question, "ระบบเพิ่มสินค้า")
End If
End Sub
End Class