Public Class Form1
Dim dbpath As String = IO.Path.GetDirectoryName(Application.ExecutablePath) & "\db.accdb"
Dim str As String = "provider=microsoft.ace.oledb.12.0;data source=" & dbpath
Dim con As New OleDb.OleDbConnection(str)
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter("select * from tb", con)
Dim cm As New OleDb.OleDbCommandBuilder(da)
Private Sub gd()
dt.Clear()
Me.DataGridView1.DataSource = Nothing
Me.DataGridView1.Rows.Clear()
da.Fill(dt)
Me.DataGridView1.DataSource = dt
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gd()
End Sub
Private Sub aad(ByVal tn As String)
Dim row As DataRow = dt.NewRow
row(1) = tn
dt.Rows.Add(row)
da.Update(dt)
gd()
End Sub
Private Sub ddel(ByVal count As Integer)
Dim i As Integer
i = 0
Do Until i = (count - 1)
dt.Rows(i).Delete()
i += 1
da.Update(dt)
Loop
gd()
End Sub
Private Function found(ByVal tn As String) As Boolean
If dt.Select("tname='" & tn & "'").Count = 0 Then
Return False
Else
Return True
End If
End Function
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim a As String = InputBox("write some name to save.")
If Trim(a.Trim).Trim = "" Then Exit Sub
If found(a) = False Then
aad(a)
Else
MsgBox("Name Exist!", MsgBoxStyle.Information, "")
End If
End Sub
Private Sub btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
If dt.Rows.Count = 0 Then Exit Sub
If Trim(txtcount.Text).Trim = "" Then Exit Sub
ddel(Val(txtcount.Text))
gd()
End Sub
End Class