locked
How to get data randomly?(c#) RRS feed

  • Question

  • User272600043 posted

    Hello everyone!

    I am trying to make a web application which shows you some words and their definitions. But I couldn't figure out how to get the words from the database? My database's name is "WordsDB".  I have 2 columns. the one is "Word" and the other one is "Definition". (Also there is an ID column). I want it to get the words randomly from the database when the applicaitons starts. And also there will be a button called "Another word". And this buttton will get another word randomly. Should I do its event same as the main event?

    I am waiting for your answers,

    Thank you for your help!

    Thursday, February 21, 2013 1:53 PM

Answers

  • User-183374066 posted

    You can get random word by tsql query

    SELECT top 1 Word, Defination FROM MyDictionaryTable order by newid()



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 21, 2013 3:57 PM
  • User260886948 posted

    Hi,

    Please try to refer to the following code:

     protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WordsDB;Integrated Security=True");
            con.Open();
            string ss = "select top 1 Word from testtable order by newid()";
            SqlCommand cmd = new SqlCommand(ss, con);
            SqlDataReader reader=cmd.ExecuteReader();
            while (reader.Read())
            {
                Response.Write(reader.GetString(0));
            }
            con.Close();
    
        }

    Hope it can help.

    Best Regards,
    Amy Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 22, 2013 3:37 AM
  • User1221318268 posted

    try this

    SELECT TOP 1 word FROM MyTable ORDER BY rand()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 22, 2013 6:46 AM

All replies

  • User-183374066 posted

    You can get random word by tsql query

    SELECT top 1 Word, Defination FROM MyDictionaryTable order by newid()



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 21, 2013 3:57 PM
  • User260886948 posted

    Hi,

    Please try to refer to the following code:

     protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WordsDB;Integrated Security=True");
            con.Open();
            string ss = "select top 1 Word from testtable order by newid()";
            SqlCommand cmd = new SqlCommand(ss, con);
            SqlDataReader reader=cmd.ExecuteReader();
            while (reader.Read())
            {
                Response.Write(reader.GetString(0));
            }
            con.Close();
    
        }

    Hope it can help.

    Best Regards,
    Amy Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 22, 2013 3:37 AM
  • User1221318268 posted

    try this

    SELECT TOP 1 word FROM MyTable ORDER BY rand()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 22, 2013 6:46 AM
  • User272600043 posted

    Thank you everyone for your answers!

    Amy, thanks for the code. It works fine!

    Have a great day!

    Saturday, February 23, 2013 12:21 PM