简体   繁体   中英

How to retrieve data RANDOMLY from SQL Server using C#

I have a set of questions in my database and I need to retrieve them in a random order every time.

Can someone please help me out with the C# code? I'm using Visual Studio 2012.

Thanks in advance.

Here's the code I'm using at the moment:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class perform_test : System.Web.UI.Page
{
public int CurrentPage
{
    get
    {
        object o = this.ViewState["_CurrentPage"];
        if (o == null)
            return 0;
        else
            return (int)o;
    }

    set
    {
        this.ViewState["_CurrentPage"] = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{


    //Response.Write(Session["Company"]);
    if (!Page.IsPostBack)
    {
        Session.Add("CorrectAnswers", 0);
       //Session.Add("Questions", 0);
        Session.Add("Answer", "");

    }
    if (repeat_exam_data.Controls.Count > 0)
    {
        for (int i = 1; i <= 4; i++)
        {
            RadioButton rad = repeat_exam_data.Controls[0].FindControl("Choice" + i.ToString()) as RadioButton;
            if (rad.Checked)
            {
                Session["Answer"] = rad.Text;
                break;
            }
        }
    }

    GetPage();
}

protected void GetPage()
{
    DataTable exam_data = new DataTable();

    exam_data = BussinessLayer.GetExamData(Session["Company"].ToString(), Session["Subject"].ToString(), Session["ExamId"].ToString());
    PagedDataSource pgds = new PagedDataSource();
    pgds.DataSource = exam_data.DefaultView;
    pgds.AllowPaging = true;
    pgds.PageSize = 1;

    pgds.CurrentPageIndex = CurrentPage;

    repeat_exam_data.DataSource = pgds;
    repeat_exam_data.DataBind();

    Cmd_Next.Enabled = !pgds.IsLastPage;
    Cmd_Finish.Enabled = pgds.IsLastPage;
    lblQno.Text = Convert.ToString((CurrentPage + 1));
    lblCorrectAnswers.Text = Session["CorrectAnswers"].ToString();
}
protected void Cmd_Next_Click(object sender, EventArgs e)
{

    CalculateMark();

    CurrentPage += 1;
    GetPage();
}
protected void Cmd_Finish_Click(object sender, EventArgs e)
{
    CalculateMark();

    string strSql = "INSERT INTO tbl_result(ExamId,StudentId,Mark) VALUES('" + Session["ExamId"] + "','" + Session["uname"] + "','" + Session["CorrectAnswers"] + "')";
    BussinessLayer.PutData(strSql);
    Response.Redirect("~/canexamresult.aspx");
}

private void CalculateMark()
{
    HiddenField ans = repeat_exam_data.Controls[0].FindControl("Answer") as     HiddenField;
    if (Session["Answer"].ToString() == ans.Value)
        Session["CorrectAnswers"] = (int)Session["CorrectAnswers"] + 1;

}
}

This code dosn't contain code responsible for feaching data. It is inside BussinessLayer.GetExamData() probably. You can achive random order for example by using

ORDER BY NEWID()

at the end of the query.

You can either use

ORDER BY NEWID();

as Sleipneir proposed, or load the whole whole set of data into a list, and eat that list in a random fashion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM