无法将信息保存到 mysql 数据库
Can't save info to mysql database
我正在尝试将信息存储到 mysql 数据库,但由于某些原因它对我不起作用。
Dim connString As String = "server=sql3.freemysqlhosting.net; userid=Censored;password=Censored;database=sql364455"
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO accounts (`user_num`, `username`, `password`) values (@1,@2,@3)"
MsgBox("1")
cmd.Parameters.AddWithValue("@1", TextBox1.Text)
cmd.Parameters.AddWithValue("@2", TextBox2.Text)
cmd.Parameters.AddWithValue("@3", TextBox3.Text)
MsgBox("2")
cmd.ExecuteNonQuery()
MsgBox("3")
MessageBox.Show("User Profile Created!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
conn.Close()
Catch ex As Exception
End Try
根据此代码,消息框 1 和 2 会弹出,但消息框 3 不会弹出。
有什么想法吗?提前致谢。
@WoeIsMe Plutonix already told you how to write it correctly (put it into brackets: [password]). And, just and advice; if you're already using MsgBox as a debugging tool, always put another one inside the catch block with the exception MsgBox(ex.ToString()), so you'll know why it is not working. – Josh Part 8
我在异常块上放了一个消息框来找出问题所在,但我发现它不起作用,因为:"Duplicate entry '0' for key 'PRIMARY' " - 它不起作用,因为已经有一个用户有了这个数字。
非常感谢所有帮助过我的人。
我用来检测问题的代码:
Dim connString As String = "server=sql3.freemysqlhosting.net; userid=username;password=password;database=sql364455"
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO accounts (user_num, username, password) values (@1,@2,@3)"
MsgBox("1")
cmd.Parameters.AddWithValue("@1", TextBox1.Text)
cmd.Parameters.AddWithValue("@2", TextBox2.Text)
cmd.Parameters.AddWithValue("@3", TextBox3.Text)
MsgBox("2")
cmd.ExecuteNonQuery()
MsgBox("3")
MessageBox.Show("User Profile Created!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
conn.Close()
Catch ex As Exception
' here
MsgBox(ex.Message)
End Try
我正在尝试将信息存储到 mysql 数据库,但由于某些原因它对我不起作用。
Dim connString As String = "server=sql3.freemysqlhosting.net; userid=Censored;password=Censored;database=sql364455"
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO accounts (`user_num`, `username`, `password`) values (@1,@2,@3)"
MsgBox("1")
cmd.Parameters.AddWithValue("@1", TextBox1.Text)
cmd.Parameters.AddWithValue("@2", TextBox2.Text)
cmd.Parameters.AddWithValue("@3", TextBox3.Text)
MsgBox("2")
cmd.ExecuteNonQuery()
MsgBox("3")
MessageBox.Show("User Profile Created!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
conn.Close()
Catch ex As Exception
End Try
根据此代码,消息框 1 和 2 会弹出,但消息框 3 不会弹出。
有什么想法吗?提前致谢。
@WoeIsMe Plutonix already told you how to write it correctly (put it into brackets: [password]). And, just and advice; if you're already using MsgBox as a debugging tool, always put another one inside the catch block with the exception MsgBox(ex.ToString()), so you'll know why it is not working. – Josh Part 8
我在异常块上放了一个消息框来找出问题所在,但我发现它不起作用,因为:"Duplicate entry '0' for key 'PRIMARY' " - 它不起作用,因为已经有一个用户有了这个数字。
非常感谢所有帮助过我的人。
我用来检测问题的代码:
Dim connString As String = "server=sql3.freemysqlhosting.net; userid=username;password=password;database=sql364455"
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO accounts (user_num, username, password) values (@1,@2,@3)"
MsgBox("1")
cmd.Parameters.AddWithValue("@1", TextBox1.Text)
cmd.Parameters.AddWithValue("@2", TextBox2.Text)
cmd.Parameters.AddWithValue("@3", TextBox3.Text)
MsgBox("2")
cmd.ExecuteNonQuery()
MsgBox("3")
MessageBox.Show("User Profile Created!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
conn.Close()
Catch ex As Exception
' here
MsgBox(ex.Message)
End Try