Module Module1
Public apath As String = IO.Path.GetDirectoryName(Application.ExecutablePath) & "\"
Public con As New OleDb.OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=" & apath & "db.accdb")
Public cmd As OleDb.OleDbCommand
Public tbl As DataTable
End Module
Public Class Form1
Private Sub execute(sql As String)
cmd = New OleDb.OleDbCommand("", con)
cmd.CommandText = sql
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
If con.State = ConnectionState.Open Then con.Close()
cmd = Nothing
End Sub
Private Sub fill()
cmd = New OleDb.OleDbCommand("", con)
cmd.CommandText = "select * from tb"
tbl = New DataTable
If con.State = ConnectionState.Closed Then con.Open()
tbl.Load(cmd.ExecuteReader)
If con.State = ConnectionState.Open Then con.Close()
tbl.Columns(0).ColumnName = "الرقم"
tbl.Columns(1).ColumnName = "الاسم"
tbl.Columns(2).ColumnName = "العمر"
dgv.Invoke(Sub() dgv.DataSource = tbl)
btnload.Invoke(Sub() btnload.Enabled = True)
If tbl.Rows.Count > 0 Then
txtsum.Invoke(Sub() txtsum.Text = tbl.Compute("SUM(العمر)", String.Empty))
End If
cmd = Nothing
tbl = Nothing
End Sub
Private Sub btnload_Click(sender As System.Object, e As System.EventArgs) Handles btnload.Click
btnload.Enabled = False
Dim th As New Threading.Thread(AddressOf fill)
If th.IsAlive = False Then
th.Start()
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub de()
Dim s As String = "delete from tb where age"
execute(s & txtop.Text & Val(txtage.Text))
btndel.Invoke(Sub() btndel.Enabled = True)
fill()
End Sub
Private Sub btndel_Click(sender As System.Object, e As System.EventArgs) Handles btndel.Click
If txtage.Text.Trim <> "" Or txtop.Text.Trim <> "" Then
If txtop.Text.Trim = "<" Or txtop.Text.Trim = ">" Or txtop.Text.Trim = "=" Then
If IsNumeric(txtage.Text) Then
btndel.Enabled = False
Dim th As New Threading.Thread(AddressOf de)
If th.IsAlive = False Then
th.Start()
End If
End If
End If
End If
End Sub
Private Sub add()
Dim s As String = "insert into tb(tname,age) values('" & TextBox1.Text & "','" & TextBox2.Text & "')"
execute(s)
btnadd.Invoke(Sub() btnadd.Enabled = True)
fill()
End Sub
Private Sub btnadd_Click(sender As System.Object, e As System.EventArgs) Handles btnadd.Click
If TextBox1.Text.Trim = "" Or TextBox2.Text.Trim = "" Then Exit Sub
btnadd.Enabled = False
Dim th As New Threading.Thread(AddressOf add)
If th.IsAlive = False Then
th.Start()
End If
End Sub
End Class