我是vb .net编程的新手,当我尝试将表单按钮连接到MySql数据库时,我遇到了以下错误: 类型为' System.ArgumentException'的未处理异常发生在System.Data.dll中 其他信息:不支持关键字。 我一直在努力解决这个错误,但我没有成功。感谢您提供的任何帮助!请参阅下面课程的代码:
导入MySql.Data.MySqlClient 公共类Form1
Dim connect As New MySqlConnection
Dim connecto As String = "Server=localhost; User Id=root; Password; Database=sist"
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
connect.ConnectionString = connecto
Try
connect.Open()
MessageBox.Show("Connected Successfully")
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label4.Text = Date.Now.ToString("MMM dd yyyy hh:mm:ss")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs)
End Sub
结束班
答案 0 :(得分:1)
connect.ConnectionString = connecto
Button1_Click子引用中的第一行是connect和connecto。但是这些对象超出了sub的范围。您可以在sub中移动实例化,它可以工作。
所以:
Dim connect As New MySqlConnection
Dim connecto As String = "Server=localhost; User Id=root; Password; Database=sist"
connect.ConnectionString = connecto
Try
connect.Open()
MessageBox.Show("Connected Successfully")
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Try
或者更好
Dim connecto As String = "Server=localhost; User Id=root; Password; Database=sist"
Using connect As New MySqlConnection(connecto)
Try
connect.Open()
MessageBox.Show("Connected Successfully")
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Try
End Using