VB操作MySQL数据库代码是用Visual Basic语言来实现对MySQL数据库的操作的代码。下面是一个示例代码:
Imports MySql.Data.MySqlClient
Public Class Form1
Dim connectionString As String = "server=localhost;port=3306;user=root;password=123456;database=testdb"
Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
Dim connection As MySqlConnection = New MySqlConnection(connectionString)
Try
connection.Open()
MessageBox.Show("Connected to MySQL Database")
connection.Close()
Catch ex As Exception
MessageBox.Show("Failed to connect: " + ex.Message)
Finally
connection.Dispose()
End Try
End Sub
Private Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
Dim connection As MySqlConnection = New MySqlConnection(connectionString)
Try
connection.Open()
Dim query As String = "INSERT INTO users (username, password) VALUES (@username, @password)"
Dim command As MySqlCommand = New MySqlCommand(query, connection)
command.Parameters.AddWithValue("@username", txtUsername.Text)
command.Parameters.AddWithValue("@password", txtPassword.Text)
command.ExecuteNonQuery()
MessageBox.Show("Data inserted successfully")
Catch ex As Exception
MessageBox.Show("Failed to insert data: " + ex.Message)
Finally
connection.Close()
connection.Dispose()
End Try
End Sub
Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
Dim connection As MySqlConnection = New MySqlConnection(connectionString)
Try
connection.Open()
Dim query As String = "SELECT * FROM users"
Dim adapter As MySqlDataAdapter = New MySqlDataAdapter(query, connection)
Dim dataSet As DataSet = New DataSet()
adapter.Fill(dataSet, "users")
dgvUsers.DataSource = dataSet.Tables("users")
Catch ex As Exception
MessageBox.Show("Failed to retrieve data: " + ex.Message)
Finally
connection.Close()
connection.Dispose()
End Try
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim connection As MySqlConnection = New MySqlConnection(connectionString)
Try
connection.Open()
Dim query As String = "UPDATE users SET password = @password WHERE username = @username"
Dim command As MySqlCommand = New MySqlCommand(query, connection)
command.Parameters.AddWithValue("@password", txtNewPassword.Text)
command.Parameters.AddWithValue("@username", txtUsernameToUpdate.Text)
command.ExecuteNonQuery()
MessageBox.Show("Data updated successfully")
Catch ex As Exception
MessageBox.Show("Failed to update data: " + ex.Message)
Finally
connection.Close()
connection.Dispose()
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim connection As MySqlConnection = New MySqlConnection(connectionString)
Try
connection.Open()
Dim query As String = "DELETE FROM users WHERE username = @username"
Dim command As MySqlCommand = New MySqlCommand(query, connection)
command.Parameters.AddWithValue("@username", txtUsernameToDelete.Text)
command.ExecuteNonQuery()
MessageBox.Show("Data deleted successfully")
Catch ex As Exception
MessageBox.Show("Failed to delete data: " + ex.Message)
Finally
connection.Close()
connection.Dispose()
End Try
End Sub
End Class
这段代码是一个简单的Windows窗体应用程序,包含了连接MySQL数据库、插入数据、查询数据、更新数据和删除数据等操作。使用MySQL的.NET连接器提供的类库来实现对MySQL数据库的操作。
请注意,这段代码仅为示例,实际使用时需要根据具体的数据库结构和需求进行修改。同时,为了运行这段代码,你需要在项目中添加对MySql.Data.dll的引用,并将其复制到输出目录中。
领取专属 10元无门槛券
手把手带您无忧上云