VB.NET MySQL致命错误但数据库接受输入

时间:2015-07-21 18:22:39

标签: mysql vb.net

执行此代码时出现致命错误,但信息仍显示在数据库中。谁能在这里看到错误?

sql.RunQuery("INSERT INTO test(test1,test2,test3) values(?FName,?LName,?DOB)")

            sql.SQLcmd.Parameters.AddWithValue("?FName", Gender.SelectedItem)
            sql.SQLcmd.Parameters.AddWithValue("?LName", Age)
            sql.SQLcmd.Parameters.AddWithValue("?DOB", RDIAge)

            sql.SQLcmd.ExecuteNonQuery()

1 个答案:

答案 0 :(得分:1)

您的代码应如下所示:

  Dim connString As String = "Database=yourDB;Data Source=localhost;";
  connString +="User Id=yourUserDb;Password=dbPass" 

  Dim conn As New MySqlConnection(connString)
  Dim cmd As New MySqlCommand()
  Try
    conn.Open()
    cmd.Connection = conn

    cmd.CommandText = "INSERT INTO test(test1,test2,test3) values(@FName,@LName,@DOB)"
    cmd.Prepare()

    cmd.Parameters.AddWithValue("@FName", Gender.SelectedItem)
    cmd.Parameters.AddWithValue("@LName", Age)
    cmd.Parameters.AddWithValue("@DOB", RDIAge)
    cmd.ExecuteNonQuery()

    conn.Close()

  Catch ex As MySqlException
      Console.WriteLine("Error: " & ex.ToString())
  End Try