Library code snippets

Placing results from DataReader into an Array

Since the connection cannot be used until a DataReader has been closed, it is quite often necessary to read the results of a DataReader into an array, then close the DataReader, THEN you can process the data in the array while you use the connection for something else. I could only get this example to work with integers but not varchars (?).

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
    void Page_Load(Object s, EventArgs e) {
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
        SqlCommand cmd = new SqlCommand("CustomerByCountry",con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@publications",SqlDbType.Int)).Value = 3;
        //cmd.Parameters.Add(new SqlParameter("@country",SqlDbType.VarChar)).Value = "USA";
       
        con.Open();
       
        ArrayList al = new ArrayList();
        SqlDataReader dr = cmd.ExecuteReader();
       
        while(dr.Read()) {
            object[] values = new object[dr.FieldCount];
            dr.GetValues(values);
            al.Add(values);
        }
       
        dr.Close();
        con.Close();
       
        foreach(object[] row in al) {
            foreach(object column in row) {
                Response.Write(column.ToString() + "<br>");
            }
        }
       
    }
</script>

Stored Procedure
------------------------------------
CREATE PROCEDURE CustomerByCountry @publications int
AS
SELECT *
FROM Customers
WHERE publications = @publications
GO

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Placing results from DataReader into an Array.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Related discussion

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!