⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datareader.aspx

📁 asp.net经典案例资料
💻 ASPX
字号:
<%@ Page Debug="true" Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
  <head>
    <title>DataReader Demo</title>
    <script language="C#" runat="server" >
    public void Page_Load()
    {
      //Create Connection object
      string dataSource = "Data Source=localhost;";
      string security = "user id=sa; password=;";
      string initialCatalog = "initial catalog=pubs;";
      string cnnString = dataSource + security + initialCatalog;
      SqlConnection connection = new SqlConnection(cnnString);

      // Create SQLCommand and DataReader
      string strSql = "select * from [authors]";
      SqlCommand command = new SqlCommand(strSql, connection);
      SqlDataReader reader = null;

      try
      {
        connection.Open();
        reader = command.ExecuteReader();
        TableRow row;
        TableCell cell;

        // Create table header
        row = new TableRow();
        Table1.Rows.Add(row);
        for(int i=0; i<reader.FieldCount; i++)
        {
          cell = new TableCell();
          cell.Text = "<b>" + reader.GetName(i) + "</b>";
          row.Cells.Add(cell);
        }

        while(reader.Read() == true)
        {
          row = new TableRow();
          Table1.Rows.Add(row);
          for(int i=0; i<reader.FieldCount; i++)
          {
            cell = new TableCell();
            cell.Text = reader[i].ToString();
            row.Cells.Add(cell);
          }
        }
      }
      catch(SqlException e)
      {
        Response.Write(e.ToString());
      } 
      finally
      {
        if(reader != null)
          reader.Close();
        connection.Close();
      }
    }
    </script>
  </head>

  <body>
  <form id="form1" runat="server">
    <h3>DataReader Demo</h3>
    <asp:Table id="Table1" runat="server" GridLines="Both"/>
  </form>
  </body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -