Handle the result of a sqlcommand select statement

Posted on 2009-02-13
Last Modified: 2012-05-06
I am sending a select statement to my database which is returning 2 columns and x rows. How do I put this result into a 2 dimensional array so I can use it later.
Dim myCommand As SqlCommand

Dim dr As SqlDataReader

**Connection opened here

myCommand = New SqlCommand("Select Person, Email From Emails", myConnection)

dr = myCommand.ExecuteReader

'TODO Fill 2 dimensional array here.

Open in new window

Question by:Un-Obtainium
    LVL 16

    Expert Comment

    Is there any specific reason for using 2 dimensional array? You could fill a dataset or you could move into into a generic list.
    LVL 17

    Accepted Solution

    something like the following should get you started.

    dim irow as integer
    dim icell as integer
    irow = 0
    icell = 0
       myarray(irow,icell) = dr(0).ToString()
       myarray(irow,icell+1) = dr(1).ToString()
       irow = irow + 1
    LVL 1

    Author Comment

    Thanks xDJR1875.

    How would I put it into a dataset?
    LVL 16

    Assisted Solution

    Like this better use DataTable
    Dim myCommand As SqlCommand
    **Connection opened here
    myCommand = New SqlCommand("Select Person, Email From Emails", myConnection)
    Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(myCommand)
    Dim dataTable As DataTable = New DataTable("Emails")

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now