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

📄 datareader.aspx

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

<html>
  <head>
    <title>DataReader Demo</title>
    <script language="C#" runat="server" >
    public void Page_Load()
    {
      /* OLEDB Code */
      //Create Connection object
      string provider = "Provider=Microsoft.Jet.OLEDB.4.0;";
      string dataSource = "Data Source=" 
        + Request.PhysicalApplicationPath + "\\Lesson31\\Diablo.mdb;";
      string cnnString = provider + dataSource;
      OleDbConnection connection = new OleDbConnection(cnnString);

      /* SQL Server Code
      //Create Connection object
      string dataSource = "Data Source=localhost;";
      string security = "user id=sa; password=;";
      string cnnString = dataSource + security;
      SqlConnection connection = new SqlConnection(cnnString);
      */

      /* OleDb Code */
      // Create Command object
      string strSql = "SELECT * from [Character]";
      OleDbCommand command = new OleDbCommand(strSql, connection);

      /* SQL Server Code
      // Create Command object
      string strSql = "SELECT * from [Character]";
      SqlCommand command = new SqlCommand(strSql, connection);
      */

      /* OleDb Code */
      // Create DataReader.
      OleDbDataReader reader = null;

      /* SqlServer Code
      // Create DataReader.
      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.Controls.Add(new LiteralControl("<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.Controls.Add(new LiteralControl(reader[i].ToString()));
            row.Cells.Add(cell);
          }
        }
        Label1.Text = "Command successful!";
      }
      /* Ole DB code */
      catch(OleDbException e)
      {
        Label1.Text = e.ToString();
      }
      /* Sql Server code
      catch(SqlException e)
      {
        Label1.Text = 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">
    </asp:Table>
    <asp:Label id="Label1" runat="server" />
  </form>
  </body>
</html>

⌨️ 快捷键说明

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