Create DataReader object from SqlCommand : DataReader « ADO.net Database « ASP.NET Tutorial

Home
ASP.NET Tutorial
1.ASP.Net Instroduction
2.Language Basics
3.ASP.net Controls
4.HTML Controls
5.Page Lifecycle
6.Response
7.Collections
8.Validation
9.Development
10.File Directory
11.Sessions
12.Cookie
13.Cache
14.Custom Controls
15.Profile
16.Configuration
17.LINQ
18.ADO.net Database
19.Data Binding
20.Ajax
21.Authentication Authorization
22.I18N
23.Mobile
24.WebPart
25.XML
ASP.NET Tutorial » ADO.net Database » DataReader 
18.24.3.Create DataReader object from SqlCommand
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

    void Page_Load(object sender, EventArgs e)
    {
        string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MSDEConnectString"]);
    
        string CommandText = "SELECT AuthorName, AuthorCity, AuthorContact_Email, AuthorWebsite FROM Author";
    
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
    
        try
        {
            myConnection.Open();
    
            SqlDataReader myReader = myCommand.ExecuteReader();
            while (myReader.Read()){
               Author p = new Author();
               p.Name = myReader.GetString(0);
               p.City = myReader.GetString(1);
               p.Email = myReader.GetString(2);
               p.Website = myReader.GetString(3);
    
               Label1.Text += p.ToString();
            }
    
            myReader.Close();
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            myConnection.Close();
        }
    }
    
    
    public class Author
    {
        public string Name;
        public string City;
        public string Email;
        public string Website;
    
        public Author()
        {}
    
        public string ToString()
        {
            string description = "";
            description = "Name : " this.Name + "<br />";
            description += "City : " this.City + "<br />";
            description += "Contact : <a href=mailto:" this.Email + ">" this.Email + "</a><br/>";
            description += "Homesite : <a href='" this.Website + "'>" this.Website + "</a><br/><br/>";
    
            return description;
        }
    }

</script>
<html>
<body>
    <asp:Label id="Label1" runat="server"></asp:Label>
</body>
</html>

File: Web.config

<configuration>
    <appSettings>
      <add key="MSDEConnectString"
           value="server=(local)\YourDatabase;database=Books;uid=YourID;pwd=letmein;" />
    
    </appSettings>
</configuration>
18.24.DataReader
18.24.1.Using the DataReader Object
18.24.2.Iterating Through A DataReader
18.24.3.Create DataReader object from SqlCommand
18.24.4.List Binding DataReader
18.24.5.Returning Multiple Resultsets
18.24.6.Working with Multiple Active Resultsets
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.