【发布时间】:2015-04-08 04:24:18
【问题描述】:
我想使用 ASP.NET 从 SQL Server 中选择/检索所有数据。
我试过这段代码...
SqlCommand cmdd = new SqlCommand("select * from comment where ID='" + st + "'", con);
SqlDataReader drr;
drr = cmdd.ExecuteReader();
while(drr.Read())
{
user_name.Text = drr["Username"].ToString();
date.Text = drr["Date_Created"].ToString();
userrcomment.Text = drr["Comment"].ToString();
}
代码工作正常,但它只能从数据库中选择一条记录,但我想选择所有记录...
还有谁能告诉我,如何仅使用一个数据读取器来使用两个/多个查询....
任何帮助将不胜感激...
更新: 问题是当查询运行时,只会显示最后一条记录,但我希望所有记录都显示在提到的 id...
更新 2 这是我的页面代码,因为我是 asp.net 的新手,所以如何获取 span 或 p 标签中的用户名和其他数据...
<asp:Panel ID="Panel1" runat="server" Height="140px" Width="378px">
<p>
<asp:Label ID="user_name" runat="server"></asp:Label> Commented on <asp:Label ID="date" runat="server"></asp:Label></p>
<asp:Label ID="userrcomment" runat="server" Height="31px" Width="378px"></asp:Label>
</asp:Panel>
【问题讨论】:
-
您传递的 id 可能仅在您的选择命令
("select * from comment where ID='" + st + "'", con);中检索一条记录@ -
只有最后的值才会显示在文本框中。
-
删除你的 where 子句,你会得到一切
-
我在同一个 ID 上有多个记录,所以逻辑上它会从数据库中选择 ID 为 8 的所有记录,但它只会选择一个...
-
你调试了吗?因为只有最后一行的值会显示在文本框中。
标签: asp.net sql-server