//Arquivo de exemplo: ExecuteReader.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class ExecuteReader : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ConnectionStringSettings getString = WebConfigurationManager.ConnectionStrings["nwind"] as ConnectionStringSettings;
            if (getString != null)
            {
                string sSql = "select CategoryID,CategoryName from Categories";
                using (SqlConnection conn = new SqlConnection(getString.ConnectionString))
                {
                    SqlDataReader r = null;
                    SqlCommand cmd = new SqlCommand(sSql, conn);
                    try
                    {
                        conn.Open();
                        r = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        if (r.HasRows)
                        {
                            int categoria = r.GetOrdinal("CategoryName");
                            while (r.Read())
                            {
                                Response.Write(r.GetString(categoria).ToString() + "<br/>");
                            }
                        }
                    }
                    catch (SqlException ex)
                    {
                        Response.Write(ex.Message);
                    }
                    catch (InvalidOperationException ex)
                    {
                        Response.Write(ex.Message);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        if (r != null) r.Close();
                    }
                }
            }
        }
    }
}

