asp.net html table,c# - Show data in ASP.NET html table - Stack Overflow

本文介绍了一种使用经典ASP/PHP风格的代码来从数据库中读取数据的方法。通过创建一个公共方法,该方法打开数据库连接,执行SQL查询,遍历结果并构造HTML字符串,然后关闭连接。在ASP.NET中,可以使用等价的控件来展示这些数据,或者使用Repeater控件结合自定义的ItemTemplate显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Basically use the classic ASP\PHP\Spaghetti code approach.

First of all, place your code in one public method that returns a string.

The method:

public string getWhileLoopData()

{

string htmlStr = "";

SqlConnection thisConnection = new SqlConnection(dbConnection);

SqlCommand thisCommand = thisConnection.CreateCommand();

thisCommand.CommandText = "SELECT * from Test";

thisConnection.Open();

SqlDataReader reader = thisCommand.ExecuteReader();

while (reader.Read())

{

int id = reader.GetInt32(0);

string Name = reader.GetString(1);

string Pass = reader.GetString(2);

htmlStr +="

"+id+""+Name+""+Pass+""

}

thisConnection.Close();

return htmlStr;

}

Then you can use the tag in ASP.NET that is equal to

It should look something like this:

ID Name Pass

There is also the option to use an repeater control and bind the data from your DB to an Item Template of your liking.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值