首页 > 代码库 > 将从数据库中获取的数据 ,以HTML表格的形式显示

将从数据库中获取的数据 ,以HTML表格的形式显示

1.HTML页面

<body><form id="form1" runat="server"><div id="divUsers" runat="server"></div>    </form></body>

2.添加命名空间

using System.Configuration;using System.Data.SqlClient;using System.Data;using System.Text;


3. 在Page_Load事件里面加载表格

protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                string conString = ConfigurationManager.ConnectionStrings["DemosDatabaseConnectionString"].ConnectionString;                string query = "SELECT * FROM Customers";                using (SqlConnection con = new SqlConnection(conString))                {                    using (SqlCommand cmd = new SqlCommand(query))                    {                        using (SqlDataAdapter sda = new SqlDataAdapter())                        {                            cmd.Connection = con;                            sda.SelectCommand = cmd;                            using (DataTable dt = new DataTable())                            {                                sda.Fill(dt);                                StringBuilder str = new StringBuilder();                                StringBuilder str1 = new StringBuilder();                                str.Append("<table>");                                str.Append("<tr>");                                for (int k = 0; k < dt.Columns.Count; k++)                                {                                    str.Append("<th>");                                    str.Append(dt.Columns[k].ColumnName);                                    str.Append("</th>");                                }                                str.Append("<th>");                                str.Append("");                                str.Append("</th>");                                str.Append("</tr>");                                for (int i = 0; i < dt.Rows.Count; i++)                                {                                    str.Append("<tr>");                                    for (int k = 0; k < dt.Columns.Count; k++)                                    {                                        str.Append("<td>");                                        str.Append(dt.Rows[i][k]);                                        str.Append("</td>");                                    }                                    str.Append("<td>");                                    str.Append("<input type=‘text‘ ID=‘txtId" + (i + 1) + "‘ runat=‘server‘ />");                                    str.Append("</td>");                                    str.Append("</tr>");                                }                                str.Append("</table>");                                str.Append("<table>");                                str.Append("<tr>");                                for (int k = 0; k < dt.Columns.Count; k++)                                {                                    str.Append("<th>");                                    str.Append("Head");                                    str.Append("</th>");                                }                                str.Append("</tr>");                                for (int i = 0; i < dt.Rows.Count; i++)                                {                                    str.Append("<tr>");                                    for (int k = 0; k < dt.Columns.Count; k++)                                    {                                        str.Append("<td>");                                        str.Append("Test");                                        str.Append("</td>");                                    }                                    str.Append("</tr>");                                }                                str.Append("</table>");                                divUsers.InnerHtml = str.ToString();                            }                        }                    }                }            }        }
View Code

 

将从数据库中获取的数据 ,以HTML表格的形式显示