Connectivity Codes
Connectivity Codes
MySqlClient
Public Class Form1
' Initialize the connection with a connection string directly
Dim con As New MySqlConnection("server=localhost;database=vbnet;Uid=root;pwd=''")
Dim cmd As MySqlCommand
Dim id As Integer
Dim currentIndex As Integer = 0
Catch ex As Exception
MessageBox.Show("Error displaying data: " & ex.Message)
Finally
' Close the connection to free resources
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Sub
cmd.ExecuteNonQuery()
' Clear text boxes and refresh data
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
disp_data()
MessageBox.Show("Record saved successfully")
Catch ex As Exception
MessageBox.Show("Error saving record: " & ex.Message)
Finally
' Ensure the connection is closed
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Sub
' Set up the command with a WHERE clause to filter by a specific column
cmd = con.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT * FROM table1 WHERE firstname = @value" ' Replace
'column_name' with the actual column name
cmd.Parameters.AddWithValue("@value", TextBox1.Text)
Catch ex As Exception
MessageBox.Show("Error filtering data: " & ex.Message)
Finally
' Ensure the connection is closed
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Sub
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
' Set up the command to update the record with the values in TextBox1, TextBox2, and
TextBox3
cmd = con.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "UPDATE table1 SET firstname = @firstname, lastname =
@lastname, city = @city WHERE id = @id"
cmd.Parameters.AddWithValue("@firstname", TextBox1.Text)
cmd.Parameters.AddWithValue("@lastname", TextBox2.Text)
cmd.Parameters.AddWithValue("@city", TextBox3.Text)
cmd.Parameters.AddWithValue("@id", id)
Catch ex As Exception
MessageBox.Show("Error updating record: " & ex.Message)
Finally
' Ensure the connection is closed
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Sub
' Set up the command to delete the record with the selected id
cmd = con.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "DELETE FROM table1 WHERE id = @id"
cmd.Parameters.AddWithValue("@id", id)
Catch ex As Exception
MessageBox.Show("Error deleting record: " & ex.Message)
Finally
' Ensure the connection is closed
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Sub