Results 1 to 4 of 4

Thread: Output of a Reader from an SQL Select

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    87

    Output of a Reader from an SQL Select

    How do I put the output to an array?

    With this it only prints out "True" and "False".

    Code:
         Try
                Using conn As New SqlConnection(connectionstring)
                    Using cmd As New SqlCommand(SELECT * FROM table1)
                        conn.Open()
                        cmd.ExecuteNonQuery()
                        Using myReader As SqlDataReader = cmd.ExecuteReader()
                             While myReader.Read()
                                  n = n + 1
                                  a(n) = myReader.Read()
                            End While
                        End Using
                    End Using
                End Using
            Catch ex As Exception
                MsgBox(ex.Message & "  :ex error code")
            End Try

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Output of a Reader from an SQL Select

    Hmm. If your code is literally that then it doesn't get anything at all on account of the number of syntax errors!

    DataReader reads datarows sequentially so if you want a specific column value you have to ask for it. I've no idea what type a() is so we'll call it Integer for now, so ...

    a(n) = myReader.GetInt32(0)

    ... would get the Integer value from the first column in the datatable.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    87

    Re: Output of a Reader from an SQL Select

    a(n) = myReader.GetInt32(0)
    Gets a Error:

    Specified cast is not valid.
    I currently have 3 rows of data with 14 columns.

    It should produce a n=42.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Output of a Reader from an SQL Select

    As I said, I chose integer merely for illustration, as I have no idea what your declaration of a() is. The type of the array and the type of the data must be the same.

    It should produce a n=42.
    Should it? In a table of three rows, you should get an array a(0), a(1), a(2) with the values of the first entries in each row assigned respectively.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

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