nicole200718
Active member
- Joined
- Feb 2, 2011
- Messages
- 35
- Programming Experience
- Beginner
I need help with what to put in the btnDelete_Click(On the bottom of the code) can anyone help how can I delete a specific name when I select it what do i put here?
Purpose: Template for building an example to show how to
' use an OLEDBCOMMAND ExecuteReader & ExecuteNonQuery
Option Explicit On
Option Strict On
' Include the database access objects in this project
Imports System.Data.OleDb
Public Class ExecuteNonQuery
Dim strCN As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Customer.mdb"
Dim strSQL As String
Private Sub ExecuteNonQuery_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Define a connection to the database
' OleDbConnection i san object that represents
' an open connection to the data source
Dim objCon As New OleDbConnection(strCN)
' Define command
' OleDbCommand is an object that represents
' an SQL statement or stored procedure to execute
' against the data source
Dim objCmd As New OleDbCommand()
' Define DataReader
' OleDbDataReader defines a way to read a forward-only
' strem of data rows from the data soruce
Dim objReader As OleDbDataReader
' Define the SQL statement to run
strSQL = "SELECT Customer_Name FROM tblCustomers ORDER BY Customer_Name"
' Populate properties and run methods of the ObjCmd object
' objCmd.Connection = objCon
' objCmd.Connection.Open()
' objCmd.CommandText = strSQL
' Shortcut way of setting properties
With objCmd
.Connection = objCon
.Connection.Open()
.CommandText = strSQL
End With
' Execute Reader
' This runs the SQL statements and collects the results
objReader = objCmd.ExecuteReader()
' Read each record returning from the query
Do While (objReader.Read())
' Read the name on the current record from
' the table field 'Customer_Name'
Dim objField As Object = objReader.Item("Customer_Name")
' Add the name to the listBox on the GUI
lstName.Items.Add(objField)
Loop
' Close Connections and objects
objReader.Close()
objCmd.Dispose()
objCon.Close()
objCon.Dispose()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' Insert record into the database
Dim strNewName As String = txtName.Text
' Define a connection to the database
' OleDbConnection i san object that represents
' an open connection to the data source
Dim objCon As New OleDbConnection(strCN)
' Define command
' OleDbCommand is an object that represents
' an SQL statement or stored procedure to execute
' against the data source
Dim objCmd As New OleDbCommand()
' Define DataReader
' OleDbDataReader defines a way to read a forward-only
' strem of data rows from the data soruce
Dim objReader As OleDbDataReader
' Define the SQL statement to run
strSQL = "SELECT Customer_Name FROM tblCustomers WHERE Customer_Name ='" & _
strNewName & "'"
' Populate properties and run methods of the ObjCmd object
' objCmd.Connection = objCon
' objCmd.Connection.Open()
' objCmd.CommandText = strSQL
' Shortcut way of setting properties
With objCmd
.Connection = objCon
.Connection.Open()
.CommandText = strSQL
End With
' Execute Reader
' This runs the SQL statements and collects the results
objReader = objCmd.ExecuteReader()
Dim binHasRows As Boolean = objReader.HasRows
objReader.Close()
If (binHasRows) Then
' Name exists, show message
MessageBox.Show("Name already exists!")
Else
' Name not in database
' Setup the insert statment
strSQL = "INSERT INTO tblCustomers values ('" & strNewName & "')"
' Update SQL statement on command object
objCmd.CommandText = strSQL
' Execute Statement
objCmd.ExecuteNonQuery()
' Duplicate listBox
Me.lstName.Items.Add(strNewName)
End If
' Clean up
objCmd.Dispose()
objCon.Close()
objCon.Dispose()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
' DELETE FROM tableName WHERE condition
End Sub
End Class