导出数据

本文介绍了一个使用C#从数据库查询并展示数据到Web页面的方法。通过连接字符串配置数据库连接,并执行SQL查询语句来获取数据。然后遍历查询结果,并将其逐条添加到Web页面的表格中进行展示。

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

动态将数据库中的内容导出

查询Student中的所有学生记录,并在table中显示。

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;

namespace webdemo1
{
public partial class select : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string constr = "data source=.;initial catalog=TestSchool;user id=sa;password=7777777";
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
string sql = "select * from TblScore";//利用占位符
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{

TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.BorderStyle = BorderStyle.Solid;
tc.Text = reader.GetValue(0).ToString();
tr.Cells.Add(tc);

tc = new TableCell();
tc.BorderStyle = BorderStyle.Solid;
tc.Text = reader.GetValue(1).ToString();
tr.Cells.Add(tc);

tc = new TableCell();
tc.BorderStyle = BorderStyle.Solid;
tc.Text = reader.GetValue(2).ToString();
tr.Cells.Add(tc);
Table1.Rows.Add(tr);
}
}


}
cmd.ExecuteReader();//insert,delete,update用正合适
}
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值