public void WriteNotice()
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
myConnection.Open();//打开数据库
string sqlNotice = "SELECT TOP 2 [notice_ID],[notice_title], convert(varchar(10),notice_date,102) AS notice_date FROM [notice] ORDER BY notice_ID DESC";
SqlCommand cmd = new SqlCommand(sqlNotice, myConnection);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Response.Write(sdr["notice_title"]);
}
myConnection.Close();//关闭数据库
}在 Page_Load函数中调用 WriteNotice()可以显示在页面上,但是我想把输出的内容显示在表格内,怎么做啊,多谢高手了!
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
myConnection.Open();//打开数据库
string sqlNotice = "SELECT TOP 2 [notice_ID],[notice_title], convert(varchar(10),notice_date,102) AS notice_date FROM [notice] ORDER BY notice_ID DESC";
SqlCommand cmd = new SqlCommand(sqlNotice, myConnection);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Response.Write(sdr["notice_title"]);
}
myConnection.Close();//关闭数据库
}在 Page_Load函数中调用 WriteNotice()可以显示在页面上,但是我想把输出的内容显示在表格内,怎么做啊,多谢高手了!
解决方案 »
- ASP.NET连接SQLSERVER数据库问题
- 求计算日期的函数.
- 如何将项目配置为进行调试?
- 高手给力,我就送分,如何用C#或者vb.net操作事务执行二个以上(Insert或update或delete)的SQL
- 求救在二级域名之间实现共享cookies的问题:
- petshop 里面 using (SqlDataReader rdr = SQLHelper.ExecuteReader()){...}是什么意思
- 关于Datalist,救命啊
- 从WebService得到的DataSet作为xml island如何在客户端与select绑定?
- 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
- 求助,asp.net的一个基本问题
- UpdatePanel里的<asp:TextBox>的焦点失去的时候怎样更新UpdatePanel?
- using 引入System.Xml命名空间不存是什么原因?
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
myConnection.Open();//打开数据库
string sqlNotice = "SELECT TOP 2 [notice_ID],[notice_title], convert(varchar(10),notice_date,102) AS notice_date FROM [notice] ORDER BY notice_ID DESC";
SqlCommand cmd = new SqlCommand(sqlNotice, myConnection);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Response.Write("<table><tr><td>"+sdr["notice_title"]</td></tr></table>);
}
myConnection.Close();//关闭数据库
}
{
lable1.text="<table> <tr> <td>"+sdr["notice_title"] </td> </tr> </table>);
}
2. 在代码中绑定数据源到Repeater下面是简单的示例代码
页面中的Repeater如下:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr><td></td></tr> <!-- 第一行 -->
</HeaderTemplate>
<ItemTemplate>
<!-- 中间的行 -->
<tr><td><%# Eval("notice_Id") %></td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>代码如下public void WriteNotice()
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
myConnection.Open();//打开数据库
string sqlNotice = "SELECT TOP 2 [notice_ID],[notice_title], convert(varchar(10),notice_date,102) AS notice_date FROM [notice] ORDER BY notice_ID DESC";
SqlCommand cmd = new SqlCommand(sqlNotice, myConnection);
SqlDataReader sdr = cmd.ExecuteReader();
Repeater1.DataSource = sdr;
Repeater1.DataBind();
myConnection.Close();//关闭数据库
}