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. General Programming
  3. Visual Basic
  4. Extracting a single record from a SQL database

Extracting a single record from a SQL database

Scheduled Pinned Locked Moved Visual Basic
databasehelp
3 Posts 2 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
    aqzman_
    wrote on last edited by
    #1

    Hey guys, I'm having a little trouble with what I'm trying to do. I think there is just one or two lines of code that need to be entered (to what I already have), but I'm not exatly sure what it is. What I'm trying to do extract one record from a SQL database, because right now I have the SQL statement in a combobox with visable disabled, and I know that's not correct. Here is what I was thinking, and if this is totally wrong just let me know.

    Sub Temp(ByVal myConnString As String)
        Dim mySelectQuery As String = "SELECT MAX(TicketID) AS Expr1 FROM tblTicket"
        Dim myConnection As New Data.SqlClient.SqlConnection(myConnString)
        Dim myCommand As New Data.SqlClient.SqlCommand(mySelectQuery, myConnection)
        myConnection.Open()
        Dim myReader As Data.SqlClient.SqlDataReader
        myReader = myCommand.ExecuteReader()
        myReader.Close()
        myConnection.Close()
    End Sub
    

    The exact bit of data I want to extract is what would be in "Expr1". I've tried several different things, as in... Dim Number as Double ... Number = myReader... Hopefully there is just a few lines of code that can be entered to extract that data, if anyone can help me I'd really appreciate it. Thanks, aqzman

    C 1 Reply Last reply
    0
    • A aqzman_

      Hey guys, I'm having a little trouble with what I'm trying to do. I think there is just one or two lines of code that need to be entered (to what I already have), but I'm not exatly sure what it is. What I'm trying to do extract one record from a SQL database, because right now I have the SQL statement in a combobox with visable disabled, and I know that's not correct. Here is what I was thinking, and if this is totally wrong just let me know.

      Sub Temp(ByVal myConnString As String)
          Dim mySelectQuery As String = "SELECT MAX(TicketID) AS Expr1 FROM tblTicket"
          Dim myConnection As New Data.SqlClient.SqlConnection(myConnString)
          Dim myCommand As New Data.SqlClient.SqlCommand(mySelectQuery, myConnection)
          myConnection.Open()
          Dim myReader As Data.SqlClient.SqlDataReader
          myReader = myCommand.ExecuteReader()
          myReader.Close()
          myConnection.Close()
      End Sub
      

      The exact bit of data I want to extract is what would be in "Expr1". I've tried several different things, as in... Dim Number as Double ... Number = myReader... Hopefully there is just a few lines of code that can be entered to extract that data, if anyone can help me I'd really appreciate it. Thanks, aqzman

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      If you are only ever going to retrieve the first column of the first row (or that the data only has one row, and the row only has one column) then you can use ExecuteScalar() - That will save you faffing around with DataReaders. Here is an updated snippet: myConnection.Open() Dim someValue As Object = myCommand.ExecuteScalar(); myConnection.Close()


      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

      A 1 Reply Last reply
      0
      • C Colin Angus Mackay

        If you are only ever going to retrieve the first column of the first row (or that the data only has one row, and the row only has one column) then you can use ExecuteScalar() - That will save you faffing around with DataReaders. Here is an updated snippet: myConnection.Open() Dim someValue As Object = myCommand.ExecuteScalar(); myConnection.Close()


        Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

        A Offline
        A Offline
        aqzman_
        wrote on last edited by
        #3

        That worked like a charm! Thanks a lot for the help!

        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