虎克的博客

Enthusiasm Biogeography-Biodiversity Informatics-Data Sciences

DataSet绑定数据到HTML的Table

| Comments

执行效果图

Default2.aspx页面代码

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

 

<!DOCTYPE html PUBLIC "–//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>

 

<html xmlns="http://www.w3.org/1999/xhtml&quot;>

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div id="display" runat="server">Table Will Go Here</div>

 

    </form>

</body>

</html>

Default.aspx.cs后台代码

 

   1:  using System;

   2:  using System.Data;

   3:  using System.Data.SqlClient;

   4:  using System.Web.Configuration;

   5:   

   6:  public partial class Default2 : System.Web.UI.Page

   7:  {

   8:      protected void Page_Load(object sender, EventArgs e)

   9:      {

  10:          // Connection set up

  11:          String strConnection = WebConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;

  12:   

  13:          SqlConnection objConnection = new SqlConnection(strConnection);

  14:          String strSQL = "SELECT ProductName, UnitsInStock FROM Products";

  15:          // DataAdapter setup

  16:          SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConnection);

  17:          // DataSet & Adapter & Table

  18:          DataSet objDataSet = new DataSet();

  19:          objAdapter.Fill(objDataSet, "dtProducts");

  20:          String strResultsHolder;

  21:          strResultsHolder = "<table width=80% border=1>";

  22:          strResultsHolder += "<tr>";

  23:          foreach (DataColumn c in objDataSet.Tables["dtProducts"].Columns)

  24:          {

  25:              strResultsHolder += "<td>" + c.ColumnName + "</td>";

  26:          }

  27:          strResultsHolder += "</tr>";

  28:          int value, blankValue;

  29:          foreach (DataRow r in objDataSet.Tables["dtProducts"].Rows)

  30:          {

  31:              value = 100 * Convert.ToInt32(r["UnitsInStock"]) / 125;

   original link:
   <a href='http://Apiaceae.github.io/blog/2009/05/14/DataSet%E7%BB%91%E5%AE%9A%E6%95%B0%E6%8D%AE%E5%88%B0HTML%E7%9A%84Table/'>http://Apiaceae.github.io/blog/2009/05/14/DataSet%E7%BB%91%E5%AE%9A%E6%95%B0%E6%8D%AE%E5%88%B0HTML%E7%9A%84Table/</a><br/>
   &nbsp;written by <a href='http://Apiaceae.github.io'>Hooker</a>
   &nbsp;posted at <a href='http://Apiaceae.github.io'>http://Apiaceae.github.io</a>
   </p>

Comments