2010-09-26 55 views
0

問候大家,我只想問我如何檢索我的數據庫中的數據行隨機數據庫中...我能夠創建一個在線測驗,其中它顯示的問題,選擇按照連續的順序,但我想要的是,每當用戶開始測驗時,它將以隨機順序顯示問題。我使用MSSQL 2005年作爲我下面的數據庫是我的代碼..任何意見或建議被高度追捧..謝謝你,有一個偉大的日子..使用DetailsView的隨機問題

QuizPage.aspx

<asp:DetailsView ID="questionDetails" runat="server" AutoGenerateRows="False" 
       CellPadding="4" ForeColor="#333333" 
       GridLines="None" Height="50px" Width="550px"> 
       <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
       <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" /> 
       <RowStyle BackColor="#F7F6F3" CssClass="generaltext" ForeColor="#333333" /> 
       <FieldHeaderStyle BackColor="#E9ECF1" CssClass="boldtext" Font-Bold="True" 
        Width="80px" /> 
       <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
       <Fields> 
        <asp:TemplateField HeaderText="Question ID">   
         <ItemTemplate> 
             <asp:Label ID="question_id" runat="server" Text='<%# Bind("question_id") %>'></asp:Label> 
         </ItemTemplate>                    
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Question:">   
         <ItemTemplate> 
             <asp:Label ID="quiz_question" runat="server" Text='<%# Bind("quiz_question") %>'></asp:Label> 
         </ItemTemplate>                    
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Choice 1:">   
         <ItemTemplate> 
             <asp:Label ID="choice1" runat="server" Text='<%# Bind("choice1") %>'></asp:Label> 
         </ItemTemplate>                    
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Choice 2:">   
         <ItemTemplate> 
             <asp:Label ID="choice2" runat="server" Text='<%# Bind("choice2") %>'></asp:Label> 
         </ItemTemplate>                    
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Choice 3:">   
         <ItemTemplate> 
             <asp:Label ID="choice3" runat="server" Text='<%# Bind("choice3") %>'></asp:Label> 
         </ItemTemplate>                    
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Choice 4:">   
         <ItemTemplate> 
             <asp:Label ID="choice4" runat="server" Text='<%# Bind("choice4") %>'></asp:Label> 
         </ItemTemplate>                    
         </asp:TemplateField> 
       </Fields> 
       <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
       <EditRowStyle BackColor="#999999" /> 
       <AlternatingRowStyle BackColor="White" CssClass="generaltext" 
        ForeColor="#284775" /> 
      </asp:DetailsView> 

    Your Answer:&nbsp; 
      <asp:DropDownList ID="answerDropDownList" runat="server" 
       style="margin-bottom: 0px"> 
       <asp:ListItem Value="1">Answer 1</asp:ListItem> 
       <asp:ListItem Value="2">Answer 2</asp:ListItem> 
       <asp:ListItem Value="3">Answer 3</asp:ListItem> 
       <asp:ListItem Value="4">Answer 4</asp:ListItem> 
      </asp:DropDownList> 

    <asp:Button ID="buttonNext" runat="server" Text="Next" /> 

QuizPage.aspx.vb

Private Function CreateConnection() As SqlConnection 
    Dim _connectionString As String = ConfigurationManager.ConnectionStrings("LMSConnectionString").ConnectionString 
    Return New SqlConnection(_connectionString) 
End Function 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not IsPostBack Then 
     getQuestions() 
    End If 
End Sub 
Private Sub getQuestions() 
    Dim quiz_id As Integer 
    quiz_id = Session("quiz_id") 
    Dim connection As SqlConnection = CreateConnection() 
    Dim command As SqlCommand = Nothing 
    Dim dt As DataTable = New DataTable() 
    command = New SqlCommand("SELECT question_id,quiz_question, choice1, choice2, choice3, choice4, answer, quiz_id FROM tblQuizQuestion WHERE (quiz_id = @quiz_id)", connection) 
    command.Parameters.AddWithValue("@quiz_id", quiz_id) 
    Dim ad As SqlDataAdapter = New SqlDataAdapter(command) 
    ad.Fill(dt) 
    questionDetails.DataSource = dt 
    questionDetails.DataBind() 
End Sub 
Protected Sub buttonNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonNext.Click 

    Try 

     ' Save off previous answers 
     Dim dr As System.Data.DataRowView 
     dr = CType(questionDetails.DataItem, System.Data.DataRowView) 

     ' Create Answer object to save values 
     Dim a As Answer = New Answer() 
     ' a.QuestionID = dr("QuestionOrder").ToString() 
     a.CorrectAnswer = dr("answer").ToString() 
     a.UserAnswer = answerDropDownList.SelectedValue.ToString() 

     Dim al As ArrayList 
     al = CType(Session("AnswerList"), ArrayList) 
     al.Add(a) 

     Session.Add("AnswerList", al) 

    Catch ex As Exception 


     Response.Redirect("default.aspx") 
    End Try 

    If questionDetails.PageIndex = questionDetails.PageCount - 1 Then 
     ' Go to evaluate answers 
     Response.Redirect("results.aspx") 
    Else 
     questionDetails.PageIndex += 1 

    End If 

    If questionDetails.PageIndex = questionDetails.PageCount - 1 Then 
     buttonNext.Text = "Finished" 
    End If 
End Sub 

回答

1

當搜索你的問題的解決方案,我碰到這個崗位絆倒:

http://haacked.com/archive/2004/06/21/658.aspx

通過在選擇SQL語句中使用ORDER BY NEWID(),你可以隨機結果EV一次您檢索記錄。我在SQL Server 2008上嘗試過它,並且出色地爲超過100條記錄工作。因此,所有你需要做的是修改您的選擇SQL到:

SELECT question_id,quiz_question, choice1, choice2, choice3, choice4, answer, quiz_id FROM tblQuizQuestion WHERE (quiz_id = @quiz_id) ORDER BY NEWID() 

另一種方法是創建一個RandomizeDataTable函數,將隨機行的順序。如果上面的解決方案不適合你,那麼我們可以看看這個。

+0

偉大..謝謝maxthephilosopher ^^, – Kid 2010-10-06 18:46:45

1
  • 不要使用SQLDATA資源。
  • 裝入行一次,在代碼隱藏
  • shuffleList<Question>
  • 店名單在Session對象
+0

感謝您的快速回復Sir Henk,我希望您不介意..您能告訴我該怎麼做嗎?因爲老實說,我沒有得到那個「洗牌」的想法..請幫助我謝謝.. – Kid 2010-09-26 17:33:51

+0

更多關於洗牌:http://en.wikipedia.org/wiki/Swap_%28computer_science%29#Using_a_temporary_variable – citronas 2010-09-26 17:49:48

+0

我編輯過上面的代碼改變它不使用SqlDataSource,但突然我遇到了一個問題,其中一旦頁面加載它顯示的第一個問題,但是當我點擊下一個按鈕沒有輸出...我期待那裏應該仍然有下一個問題... – Kid 2010-09-27 02:52:46