dataset 操作數據庫 讀取數據並列表顯示

Default.aspx.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=hannibal-pc\\hansql;uid=sa;pwd=xiaoaiai;database=test;min pool size=10;max pool size=512");
        String sql=" select * from person ";
        SqlDataAdapter cmd = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        cmd.Fill(ds, "person");
        DataTable dt=ds.Tables[0];
        DataRowCollection drc=dt.Rows;
        String html="";
        html += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\" >";
        html += "<th>id</th>";
        html += "<th>name</th>";
        html += "<th>pwd</th>";
        foreach(DataRow dr in drc)
        {
            html += "<tr>";
            html += "<td>" + dr["id"].ToString() + "</td>";
            html += "<td>" + dr["name"].ToString() + "</td>";
            html += "<td>" + dr["pwd"].ToString() + "</td>";
            html += "</tr>";
        }
        html += "</table>";
        this.table_html.InnerHtml = html;
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章