Results 1 to 6 of 6

Thread: [RESOLVED] Select random field with No index

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    26

    Resolved [RESOLVED] Select random field with No index

    Hi
    I have a sqlserver table and a select statment. With the result of the sql stayment i want to select a random register. How can i achive that if i don't have a index field?
    Thank you
    There is my statment code
    Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\novapastavb\novapasta.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

    Dim command As New SqlCommand("SELECT count(login) as reclames FROM login where login=@nome and passe=@passe", connection)
    command.Parameters.AddWithValue("@nome", TextBox1.Text)
    command.Parameters.AddWithValue("@passe", TextBox2.Text)

    connection.Open()
    Dim reader As SqlDataReader = command.ExecuteReader

    While reader.Read()
    MessageBox.Show(String.Format("Existem Muitos clientes Nome {0} ", reader("reclames")))
    End While

    reader.Close()

    connection.Close()

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Select random field with No index

    Moved From The CodeBank

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Select random field with No index

    I'm not sure what you mean by register. That word can have serveral different definitions, but none of them seem to apply to the example you posted. Not sure of the translation, but that looks like you are querying on name (is that nome?) and password (is that passe?) from a login table. From that, you should get a reader with only a single line, unless the login table is such that there are multiple records with the same name and password, which would be kind of weird. Since you only return a single value for what should be a single line, ExecuteScalar will have superior performance to ExecuteReader, as you don't really need a datareader.

    On the other hand, I could be totally misunderstanding, and I probably am.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    26

    Re: Select random field with No index

    Thanky you Shaggy Hiker for your time and sorry about my english and my poor explanation of the problem.
    When i use a select statment and it returns more than one record i want to show (on a label ) not the first record but a random record.
    Thank you

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Select random field with No index

    Try something like this

    sql Code:
    1. SELECT TOP 1 login as reclames FROM login where login=@nome and passe=@passe ORDER BY NEWID()

    If there is more then one return you will get a random one back.


    I orgianlly posted this here:
    http://www.vbforums.com/showthread.p...27#post3316427
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    26

    Re: [RESOLVED] Select random field with No index

    Many thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width