直接把HTML代碼寫入即可,像這樣:<% @Import namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %><HTML>
<HEAD>
<title>使用 SqlDataReader 讀取數據示例</title>
<script language="C#" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
string Sql = "SELECT LastName, FirstName FROM Employees"; SqlConnection thisConnection = new SqlConnection(ConnectionString);
SqlCommand thisCommand = new SqlCommand(Sql, thisConnection);
thisCommand.CommandType = CommandType.Text; try
{
// 打開數據庫連接
thisCommand.Connection.Open(); // 執行SQL語句,並返回DataReader對像
SqlDataReader dr = thisCommand.ExecuteReader(); // 以粗體顯示標題
myLabel.Text = "<b>LastName FirstName</b><br>"; // 循環讀取結果集
while(dr.Read())
{
// 讀取兩個列值並輸出到Label中
myLabel.Text += dr["LastName"] + " " + dr["FirstName"] + "<br>";
}
// 關閉DataReader
dr.Close();
}
catch(SqlException ex)
{
// 異常處理
Response.Write(ex.ToString());
}
finally
{
// 關閉數據庫連接
thisCommand.Connection.Close();
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<h3>使用 SqlDataReader 讀取數據示例</h3>
顯示內容:<br>
<asp:Label id="myLabel" runat="server"></asp:Label>
</form>
</body>
</HTML>
<% @Import Namespace="System.Data.SqlClient" %><HTML>
<HEAD>
<title>使用 SqlDataReader 讀取數據示例</title>
<script language="C#" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
string Sql = "SELECT LastName, FirstName FROM Employees"; SqlConnection thisConnection = new SqlConnection(ConnectionString);
SqlCommand thisCommand = new SqlCommand(Sql, thisConnection);
thisCommand.CommandType = CommandType.Text; try
{
// 打開數據庫連接
thisCommand.Connection.Open(); // 執行SQL語句,並返回DataReader對像
SqlDataReader dr = thisCommand.ExecuteReader(); // 以粗體顯示標題
myLabel.Text = "<b>LastName FirstName</b><br>"; // 循環讀取結果集
while(dr.Read())
{
// 讀取兩個列值並輸出到Label中
myLabel.Text += dr["LastName"] + " " + dr["FirstName"] + "<br>";
}
// 關閉DataReader
dr.Close();
}
catch(SqlException ex)
{
// 異常處理
Response.Write(ex.ToString());
}
finally
{
// 關閉數據庫連接
thisCommand.Connection.Close();
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<h3>使用 SqlDataReader 讀取數據示例</h3>
顯示內容:<br>
<asp:Label id="myLabel" runat="server"></asp:Label>
</form>
</body>
</HTML>
把要显示的东西做成一个html的模板,查询的时候先读到模板内容,再替换你要显示的字段,循环显示记录!
用response.write方法
while(...)
{
//数据库内容
response.writeline(...)
}
SqlConnection connchange=new SqlConnection(@"Server=.;database=Outlook;uid=sa;pwd=sa");
SqlCommand cmdchage=new SqlCommand("select S_Title,S_Con,S_sTime,S_eTime from SCHEDULE ",connchange);
connchange.Open();
SqlDataReader drchange=cmdchage.ExecuteReader();
while(drchange.Read())
{
TextBox tb = new TextBox();//textbox
Label mylabel = new Label();//title
Label mylabel2=new Label(); //plan
DropDownList dd=new DropDownList();
dd.Attributes["algin"]="right";
Label mylabel1 = new Label();//<br>
tb.Width=504;
tb.Height=191;
tb.TextMode=TextBoxMode.MultiLine; TableRow row1 = new TableRow();
TableRow row2=new TableRow();
TableCell cell1 = new TableCell();
cell1.Attributes["algin"]="left";
TableCell cell2=new TableCell();
TableCell cell3 = new TableCell();
cell3.Attributes["align"] = "right";
mylabel2.Text="进度率 : ";
mylabel.Text=drchange["S_Title"].ToString();
tb.Text=drchange["S_Con"].ToString();
cell1.Controls.Add(mylabel);
cell2.Controls.Add(tb);
cell2.Attributes["colspan"] = "2";
cell3.Controls.Add(mylabel2);
cell3.Controls.Add(dd); row1.Cells.Add(cell1);
row1.Cells.Add(cell3);
row2.Cells.Add(cell2); this.Table1.Rows.Add(row1);
this.Table1.Rows.Add(row2); } connchange.Close();