ASP.NET Tutorial/ADO.net Database/OdbcDataReader — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:56, 26 мая 2010

Create OdbcDataReader object from OdbcCommand

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MySQLConnectString"]);
        string CommandText = "SELECT AuthorName, AuthorCity, AuthorContact_Email, AuthorWebsite FROM Author";
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
        OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
        try
        {
            myConnection.Open();
            OdbcDataReader myReader = myCommand.ExecuteReader();
        if (myReader.HasRows)
        {
            while (myReader.Read())
            {
               for (int i=0; i<=(myReader.FieldCount-1); i++)
               {
                  if (myReader.IsDBNull(i))
                  {
                     Label1.Text += "Warning: Field " + i + " is NULL.";
                  }
               }
               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();
            }
         } else {
            Label1.Text = "No rows returned";
         }
            if (!(myReader.IsClosed))
            {
              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="MySQLConnectString"
           value="driver={MySQL ODBC 3.51 Driver};server=localhost;database=Books;uid=YourID;pwd=letmein;" />
    </appSettings>
</configuration>


Iterating Through An OdbcDataReader

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MySQLConnectString"]);
        string CommandText = "SELECT AuthorName, AuthorCity, AuthorContact_Email, AuthorWebsite FROM Author";
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
        OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
        try
        {
            myConnection.Open();
            OdbcDataReader 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="MySQLConnectString"
           value="driver={MySQL ODBC 3.51 Driver};server=localhost;database=Books;uid=YourID;pwd=letmein;" />
    </appSettings>
</configuration>