我有一个我正在构建的网络应用程序,主要有多个选择问题和一些简短的答案。所有答案都通过存储过程放入数据库中,然后使用不同的过程进行提取和纠正。虽然简短答案问题包含在查询中,但它们没有打印到页面,而是我将它们也存储在会话变量中并打印在各自的文本框中,问题是如果会话,审阅者无法再看到它们超时了。所以我想知道在VB.net中如何从查询中提取特定行,然后从那里使用它。
这就是我目前的
Dim P2Ans As String
P2Ans = Session("Part2")
Dim Part2Txt As New ArrayList(P2Ans.Split("|"c))
Normal.Text = Part2Txt(0)
Normal2.Text = Part2Txt(1)
Normal3.Text = Part2Txt(2)
我想要的是这样的
Dim P2Ans As String
P2Ans = Select.Tables(Answers).Where("QuestionID =X") 'from there take the answer out of the column "Question Answer"
Dim Part2Txt As New ArrayList(P2Ans.Split("|"c))
Normal.Text = Part2Txt(0)
Normal2.Text = Part2Txt(1)
Normal3.Text = Part2Txt(2)
我不知道这是可能还是最佳做法,所以任何建议都会很棒。
我的回答:这样的事情
Dim queryString As String = "SELECT questionAnswer FROM Submissions where submissionid =" + SID + "and questionNumber = 1"
Using connection As New SqlConnection(My.Settings.ConnString)
Dim command As New SqlCommand(queryString, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
答案 0 :(得分:1)
您可以使用SqlCommand对象,将查询传递给它(即SELECT回答FROM回答WHERE questionid ='x'),然后使用SqlDataReader检索答案并将其保存到表单上的变量和/或控件中。 / p>