无法将信息保存到mysql数据库

时间:2021-10-25 13:18:59

I'm trying to store info to a mysql database, but for some reason it's not working for me.

我正在尝试将信息存储到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

According to this code the message boxes 1 and 2 is popping up but 3 is not.

根据此代码,消息框1和2弹出,但3不弹出。

Any idea? thanks in advance.

任何想法?提前致谢。

1 个解决方案

#1


0  

@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

@WoeIsMe Plutonix已经告诉你如何正确编写它(把它放在括号中:[密码])。而且,只是和建议;如果您已经使用MsgBox作为调试工具,请始终将另一个放在catch块中,并使用MsgBox(ex.ToString()),因此您将知道它为什么不起作用。 - Josh Part 8

I've put a msgbox on the exception block to find out what is the problem and I found that it didn't worked because: "Duplicate entry '0' for key 'PRIMARY' " - it didn't worked because there was already a user with this number.

我在异常块上放了一个msgbox来找出问题是什么,我发现它没有用,因为:“重复条目'0'用于键'PRIMARY'” - 它没有用,因为已经有了拥有此号码的用户。

thanks alot for all the people who helped me.

非常感谢所有帮助过我的人。

the code I used to detect the problem:

我用来检测问题的代码:

    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

#1


0  

@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

@WoeIsMe Plutonix已经告诉你如何正确编写它(把它放在括号中:[密码])。而且,只是和建议;如果您已经使用MsgBox作为调试工具,请始终将另一个放在catch块中,并使用MsgBox(ex.ToString()),因此您将知道它为什么不起作用。 - Josh Part 8

I've put a msgbox on the exception block to find out what is the problem and I found that it didn't worked because: "Duplicate entry '0' for key 'PRIMARY' " - it didn't worked because there was already a user with this number.

我在异常块上放了一个msgbox来找出问题是什么,我发现它没有用,因为:“重复条目'0'用于键'PRIMARY'” - 它没有用,因为已经有了拥有此号码的用户。

thanks alot for all the people who helped me.

非常感谢所有帮助过我的人。

the code I used to detect the problem:

我用来检测问题的代码:

    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