Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class Form1
Private constr As String = _
"Provider=microsoft.jet.oledb.4.0;" & _
"data source=D:\Documents\Visual Studio 2008\Projects\Phamacy\WindowsApplication2\bin\Debug\data.mdb"
Private cmd As OleDbCommand
Private conn As OleDbConnection
Private Sub showdata()
Dim conn As New OleDbConnection(constr)
conn.Open()
Dim sql As String = "select * from customer"
Dim cmd As New OleDbCommand(sql, conn)
Dim adapter As New OleDbDataAdapter(cmd)
Dim data As New DataSet()
adapter.Fill(data, "cat")
DataGridView1.DataSource = data.Tables("cat")
conn.Close()
conn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Showdata()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New OleDbConnection(constr)
conn.Open()
If txtid.Text = "" Then
MessageBox.Show("Please input (id)")
Me.txtid.Focus()
Exit Sub
End If
If txtname.Text = "" Then
MessageBox.Show("Please input (name)")
Me.txtname.Focus()
Exit Sub
End If
If txtadd.Text = "" Then
MessageBox.Show("Please input (add)")
Me.txtadd.Focus()
Exit Sub
End If
If MaskedTextBox1.Text = "" Then
MessageBox.Show("Please input (Tel)")
Me.MaskedTextBox1.Focus()
Exit Sub
End If
If txtpha.Text = "" Then
MessageBox.Show("Please input (pha)")
Me.txtpha.Focus()
Exit Sub
End If
Dim sql As String = "INSERT INTO " & _
"customer(cusid," & _
"cusname," & _
"cusadd," & _
"custel," & _
"cuspha)" & _
"VALUES(@id,@name,@add,@tel,@pha)"
Dim cmd As New OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("id", txtid.Text)
cmd.Parameters.AddWithValue("name", txtname.Text)
cmd.Parameters.AddWithValue("add", txtadd.Text)
cmd.Parameters.AddWithValue("tel", MaskedTextBox1.Text)
cmd.Parameters.AddWithValue("pha", txtpha.Text)
Dim afftectedRows As Integer = cmd.ExecuteNonQuery()
If (afftectedRows = -1) Then
MsgBox(" ผิดพลาด! " & vbNewLine & " ไม่สามารถเก็บได้")
End If
conn.Close()
showdata()
txtid.Text = ""
txtname.Text = ""
txtadd.Text = ""
MaskedTextBox1.Text = ""
txtpha.Text = ""
End Sub
Dim ordID As String = dataGridView1.SelectedRows(0).Cells(0).Value.ToString()
Dim sql As String = "DELETE FROM Orders WHERE OrderID =" & ordID
_command.CommandText = sql
_command.ExecuteNonQuery()
ลองประกาศตัวแปรอ้างไปที่อ็อปเจก CurrencyManager
Dim WithEvents objCurrencyManager As CurrencyManager
แล้วก็ใช้
objCurrencyManager.RemoveAt(objCurrencyManager.Position)