DataList显示数据库表的字段名
/// <summary>/// 返回表名下所有字段
/// </summary>
/// <param name="SqlConnectionStr"></param>
/// <returns></returns>
public static void GetAllColumn(string source, string tableName,DataList dl)
{
List<string> list = new List<string>();
//打开连接
SqlConnection sqlcn = new SqlConnection(source);
sqlcn.Open();
SqlCommand sqlcmd = new SqlCommand("SELECT TOP 1 * FROM [" + tableName + "]", sqlcn);
SqlDataReader dr = sqlcmd.ExecuteReader();
for (int i = 0; i < dr.FieldCount; i++)
{
list.Add(dr.GetName(i));//数据库的字段名
}
dl.DataSource = list;
dl.DataBind();
sqlcn.Close();
}
<asp:DataList ID="DataList2" runat="server" Width="97px">
<ItemTemplate>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 100px; height: 19px">
<asp:CheckBox ID="CheckBox1" runat="server" /></td>
<td style="width: 100px; height: 19px">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("怎么绑定") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
我在DataList用Label显示数据库某一张表下的所有字段的名称(比如在页面上显示"name",而不是字段名对应的值。)。望各位朋友指点!
效果图为:
--------------------编程问答-------------------- 木有看到图 --------------------编程问答-------------------- 不会上图的话,请参考:
http://topic.csdn.net/u/20110903/18/7b87292a-527d-41f1-af5e-98401cd82d16.html
补充:.NET技术 , ASP.NET