Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to select 10 random records from my SQLServer DataBase?

How to select 10 random records from my SQLServer DataBase?

Scheduled Pinned Locked Moved ASP.NET
csharpdatabasetutorialquestionlounge
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andylaw
    wrote on last edited by
    #1

    :)Can you give me some instances? Better in C#! Thanks! One is never too old to learn

    F S 2 Replies Last reply
    0
    • A andylaw

      :)Can you give me some instances? Better in C#! Thanks! One is never too old to learn

      F Offline
      F Offline
      fadee
      wrote on last edited by
      #2

      Fill up the dataset (ds) with data Random R =new Random(); int I; int MaxLimit = ds.Tables[0].Rows.Count; int MinLimit = 0; for(I = 0; I < 10; I++) Response.Write(ds.Tables[0].Rows[R.Next(MinLimit,MaxLimit)]["name"]; --------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done.

      1 Reply Last reply
      0
      • A andylaw

        :)Can you give me some instances? Better in C#! Thanks! One is never too old to learn

        S Offline
        S Offline
        Seagiles
        wrote on last edited by
        #3

        I would do it in your SQL query rather than in C#. select top 10 * from products order by newid() Try that on your northwind database and look at the difference if you leave off the order by newid(). Doing it with the SQL means that you don't have to pull back everything in your DS if your only looking for 10 random records. KingXango

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups