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

📄 dataset.aspx

📁 C#开发者可使用的经典案例集,源自于ASP.NET经典范例50讲
💻 ASPX
字号:
<%@ Page Debug="true" Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
  <head>
    <title>DataSet 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 Data Command
      string strSql = "select * from [authors]";
      SqlCommand command = new SqlCommand(strSql, connection);

      // Create Data Adapter
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.SelectCommand = command;

      // Create DataSet
      DataSet dataSet = new DataSet();

      try
      {
        connection.Open();
        adapter.Fill(dataSet);
      }
      catch(SqlException e)
      {
        Label1.Text = e.ToString();
      } 
      finally
      {
        connection.Close();
      }

      DataTable dataTable = dataSet.Tables[0];

      TableRow row;
      TableCell cell;

      // Create table header
      row = new TableRow();
      Table1.Rows.Add(row);
      foreach(DataColumn dataColumn in dataTable.Columns)
      {
        cell = new TableCell();
        cell.Controls.Add(new LiteralControl("<b>" + dataColumn.ColumnName + "</b>"));
        row.Cells.Add(cell);
      }

      // Fill table
      foreach(DataRow dataRow in dataTable.Rows)
      {
        row = new TableRow();
        Table1.Rows.Add(row);
        object[] fields = dataRow.ItemArray;
        foreach(object o in fields)
        {
          cell = new TableCell();
          cell.Controls.Add(new LiteralControl(o.ToString()));
          row.Cells.Add(cell);
        }
      }
      Label1.Text="Connection successful!";
    }
    </script>
  </head>

  <body>
  <form id="form1" runat="server">
    <h3>DataSet 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 + -