Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Imports System.Data.OleDb
Public Class Form1
'global declaration
Dim conString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\houssem\Desktop\notice\soccerDB.mdb;"
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(conString)
Dim adapter As OleDb.OleDbDataAdapter
Dim cmd As OleDb.OleDbCommand
Dim dt As DataTable = New DataTable()
Private Function HasExist(ByVal SQL As String) As Boolean
Try
con.Open()
cmd = New OleDb.OleDbCommand(SQL, con)
Dim count As Int32 = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
If (count >= 1) Then
MsgBox("Value Has Exist")
Return True
Else
Return False
End If
cmd.dispose()
' con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
Return False
' Retreive()
End Try
End Function
Private Sub LoadComboBox()
ComboBox1.Items.clear()
con.Open()
Dim cm As New OleDbCommand("SELECT * FROM soccerTB", con)
Dim dr As OleDbDataReader = cm.ExecuteReader
While dr.Read
ComboBox1.Items.Add(dr.GetString(1))
End While
dr.Close()
con.Close()
End Sub
Private Sub populate(ByVal name As String)
'sql stmt
Dim sql As String = "INSERT INTO soccerTB(P_NAME) VALUES (@NAMES)"
cmd = New OleDb.OleDbCommand(sql, con)
'add parameters
cmd.Parameters.AddWithValue("@NAMES", name)
'open connection and insert
Try
If HasExist("SELECT COUNT(*) FROM soccerTB WHERE P_NAME='" & trim(nameTxt.Text) & "'") = True Then
Exit Sub
End If
con.Open()
'execute statement
If cmd.ExecuteNonQuery() > 0 Then
MsgBox("successfully entred data")
End If
'clear text
nameTxt.Text = ""
con.Close()
'Retreive()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End Sub
Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click
'populate()
populate(nameTxt.Text)
LoadComboBox()
End Sub
'retreive from db.
Private Overloads Sub Update(ByVal value As String, ByVal name As String)
Dim sql As String = "UPDATE soccerTB SET P_NAME='" + value + "'WHERE P_NAME='" + name + "'"
cmd = New OleDb.OleDbCommand
'execute
Try
con.Open()
adapter.UpdateCommand = con.CreateCommand()
adapter.UpdateCommand.CommandText = sql
If adapter.UpdateCommand.ExecuteNonQuery() > 0 Then
MsgBox("successfuly updated")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
' Retreive()
End Try
End Sub
Private Sub updateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateBtn.Click
Update(nameTxt.Text, ComboBox1.SelectedItem)
LoadComboBox()
End Sub
Public Sub delete(ByVal name As String)
Dim sql As String = "DELETE FROM soccerTB WHERE P_NAME='" + name + "'"
cmd = New OleDb.OleDbCommand(sql, con)
'execute
Try
con.Open()
adapter.DeleteCommand = con.CreateCommand()
adapter.DeleteCommand.CommandText = sql
If MessageBox.Show("etes vous sur ?", "DELETE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = DialogResult.OK Then
If (adapter.DeleteCommand.ExecuteNonQuery > 0) Then
nameTxt.Text = ""
MsgBox("successfully Deleted")
End If
End If
'close connection
con.Close()
' Retreive()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End Sub
Private Sub deleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteBtn.Click
delete(ComboBox1.SelectedItem)
LoadComboBox()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadComboBox()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
nameTxt.Text = ComboBox1.SelectedItem
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sortirBtn.Click
Me.Close()
End Sub
End Class