0

我在输入乘法输入时遇到问题

Try
    Dim StrSQL As String = "INSERT INTO boranga" & _
                           "(IdBorangA,Answers)" & _
                           "VALUES (@B_IdA,@B_Answer);"
    Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn.open)
    myCommand.CommandType = CommandType.Text
    Dim parameterB_IdA As MySqlParameter = New MySqlParameter("@B_IdA", MySqlDbType.VarChar, 300)
    parameterB_IdA.Value = Label1.Text

    Dim parameterB_Answer As MySqlParameter = New MySqlParameter("@B_Answer", MySqlDbType.VarChar, 300)
    parameterB_Answer.Value = TextBox1.Text

    With myCommand.Parameters
        .Add(parameterB_IdA)
        .Add(parameterB_Answer)
    End With

    Dim result As MySqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    MsgBox("Tersimpan", vbYes, "boranga")

Catch SqlEx As MySqlException
    Throw New Exception(SqlEx.Message.ToString())
End Try

此当前代码用于为每个表“IdBorangA”和“Answer”只输入一个输入,我尝试稍微调整一下

Try
    Dim StrSQL As String = "INSERT INTO boranga" & _
                           "(IdBorangA,Answers)" & _
                           "VALUES (@B_IdA,@B_Answer);"
    Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn.open)
    myCommand.CommandType = CommandType.Text
    Dim parameterB_IdA As MySqlParameter = New MySqlParameter("@B_IdA", MySqlDbType.VarChar, 300)
    parameterB_IdA.Value = Label1.Text
    parameterB_IdA.Value = Label2.Text

    Dim parameterB_Answer As MySqlParameter = New MySqlParameter("@B_Answer", MySqlDbType.VarChar, 300)
    parameterB_Answer.Value = TextBox1.Text
    parameterB_Answer.Value = TextBox2.Text

    With myCommand.Parameters
        .Add(parameterB_IdA)
        .Add(parameterB_Answer)
    End With

    Dim result As MySqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    MsgBox("Tersimpan", vbYes, "boranga")

Catch SqlEx As MySqlException
    Throw New Exception(SqlEx.Message.ToString())
End Try

通过为 IdBorangA 添加更多参数“parameterB_IdA.Value = Label1.Text”和为 Answer 添加更多参数“parameterB_Answer.Value = TextBox2.Text”

但我得到的结果是表格只填充了来自 Label2.text 和 Textbox2.text 的数据。

我不知道出了什么问题..如果你们有任何想法可以帮助我解决这个问题。随意回答:)

4

1 回答 1

0

这条线是错误的

Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn.open) 

正确的是

Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn) 

此查询是一个INSERT,因此您需要调用ExecuteNonQuery而不是 ExecuteReader,它用于使用 MySqlDataReader 进行数据检索

而且,当然,您在数据库中只写入一条记录而不是两条,您只是在将命令发送到数据库之前更改参数的值,只会写入最后一个值。

于 2012-06-06T11:17:55.823 回答