我正在创建多项选择题系统。到目前为止,我创建了这4个表和1个视图。
这些表是tblQuestion,tblAnswer,tblQuiz,tblResult和tblResultDetail。 tblQuestion用于存储问题,tblAnswer用于存储问题的答案,tblResult用于记录每个回答测验的用户,并将用户答案存储在TblResultDetails中。
根据以下代码,从视图中读取数据。我使用1,2,3,4,因为它是视图的列名。我这样做是为了随机化答案。
Sub soalan()
conn.Open()
Dim myArr(3) As String
Dim cmd As New SqlCommand("Select * From view_Soalan Where QuestionID=@IdSoalan", conn)
cmd.Parameters.AddWithValue("@IdSoalan", Counter)
Dim dr1 As SqlDataReader
dr1 = cmd.ExecuteReader
If dr1.Read() Then
Me.lblSoalan.Text = dr1("QuestionTxt")
Me.RadioButton1.Text = dr1("1")
myArr(0) = dr1("1")
Me.RadioButton2.Text = dr1("2")
myArr(1) = dr1("2")
Me.RadioButton3.Text = dr1("3")
myArr(2) = dr1("3")
Me.RadioButton4.Text = dr1("4")
myArr(3) = dr1("4")
Dim answerId As String
If Me.RadioButton1.Checked = True Then
answerId = dr1("1")
ElseIf Me.RadioButton2.Checked = True Then
answerId = dr1("2")
ElseIf Me.RadioButton3.Checked = True Then
answerId = dr1("3")
ElseIf Me.RadioButton4.Checked = True Then
answerId = dr1("4")
End If
'Dim jawapan As Integer = CInt(answerId)
Session("jaw") = answerId
Else
conn.Close()
Counter += 1
soalan()
End If
conn.Close()
End Sub
Sub bersih()
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
soalan()
End Sub
Sub masuk()
conn.Open()
Dim cmdGetId As New SqlCommand("Select MAX(ResultId) From TblResult", conn)
cmdGetId.ExecuteNonQuery()
Dim drBaca As SqlDataReader
drBaca = cmdGetId.ExecuteReader
While drBaca.Read
Dim maxID As Integer = drBaca(0)
Session("maximum") = maxID
End While
conn.Close()
conn.Open()
Dim cmdInsert As New SqlCommand("Insert into TblResultDetail (ResultDetail_Result_Id,ResultDetail_Answer_Id) values ('" & Session("maximum") & "','" & Session("jaw") & "')", conn)
cmdInsert.ExecuteNonQuery()
conn.Close()
End Sub
End Class
我收到了错误
将varchar值'table'转换为数据时转换失败 输入int。
在cmdInsert命令中。我知道我不能直接将会话(“下巴”)插入表中。那么如何更换呢?
答案 0 :(得分:0)
我认为您在某些字段中存储了错误的值。
使用此
Public Module MyExtensions
<System.Runtime.CompilerServices.Extension()> _
Public Function IsInteger(ByVal value As String) As Boolean
If String.IsNullOrEmpty(value) Then
Return False
Else
Return Integer.TryParse(value, Nothing)
End If
End Function
<System.Runtime.CompilerServices.Extension()> _
Public Function ToInteger(ByVal value As String) As Integer
If value.IsInteger() Then
Return Integer.Parse(value)
Else
Return 0
End If
End Function
End Module
然后
value.ToInteger()
&lt; - 如果不是整数,则返回0
您的代码中还有其他错误。您应该在以
开头的查询中使用ExecuteScalar而不是ExecuteNonQuery“选择MAX(ResultId).... ”
Dim cmdGetId As New SqlCommand("Select MAX(ResultId) From TblResult", conn)
Dim maxID As Integer= cmdGetId.ExecuteScalar
Session("maximum") = maxID
当您的查询返回单个值时,通常会使用ExecuteScalar 。
ExecuteNonQuery 通常用于没有结果的SQL语句(UPDATE,INSERT等)。
参考this
答案 1 :(得分:0)
在您的查询中,您引用了整数:
...值(&#39;&#34;&amp;会话(&#34;最大&#34;)&amp;&#34;&#39;,&#39;&#34;
只需删除引号即可。此外,您应该使用参数来防止SQL注入。
即。
Dim cmdInsert As New SqlCommand("Insert into TblResultDetail (ResultDetail_Result_Id,ResultDetail_Answer_Id) values (@max, @jaw)", conn)
cmdInsert.Parameters.AddWithValue("@max", Session("maximum"))
cmdInsert.Parameters.AddWithValue("@jaw", Session("jaw"))