到目前为止,我一直在实现DML查询以成功更新数据库表,但在这种特殊情况下,我遇到了一个小问题。我已经设置了DML查询中使用的参数,但是我得到了一个异常
没有给出一个或多个参数的值
以下是代码:
Dim SQLQuery As String = <sql>
UPDATE QuestionAndAnswers
SET QuestionDescription = @QuestionDescription,
WorksheetID = @WorksheetID,
CorrectAnswer = @CorrectAnswer,
IncorrectAnswer1 = @IncorrectAnswer1,
IncorrectAnswer2 = @IncorrectAnswer2,
IncorrectAnswer3 = @IncorrectAnswer3
WHERE QuestionID = @QuestionID
</sql>.Value
Dim cmd = New OleDbCommand(SQLQuery, Connection)
Connection.Open()
cmd.Parameters.Add("@QuestionDescription", SqlDbType.Text).Value = txtQuestionDescription.Text.Trim
cmd.Parameters.Add("@WorksheetID", SqlDbType.Text).Value = txtWorksheetID.Text.Trim
cmd.Parameters.Add("@CorrectAnswer", SqlDbType.Text).Value = txtCorrectAnswer.Text.Trim
cmd.Parameters.Add("@IncorrectAnswer1", SqlDbType.Text).Value = txtIncorrectAns1.Text.Trim
cmd.Parameters.Add("@IncorrectAnswer2", SqlDbType.Text).Value = txtIncorrectAns2.Text.Trim
cmd.Parameters.Add("@IncorrectAnswer3", SqlDbType.Text).Value = txtIncorrectAns3.Text.Trim
cmd.Parameters.AddWithValue("@QuestionID", txtQuestionID.Text.Trim)
cmd.ExecuteNonQuery()
'If rowsInserted > 0 Then
' MessageBox.Show("Record successfully updated", "Updated")
' ShowDataGridContentsWorksheetTable()
'Else
' MessageBox.Show("Failed to update the record", "Failure")
'End If
Connection.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.RetryCancel)
Connection.Close()
End Try
我感谢任何帮助。