主题:初学。net请问读取数据库前两条,按照此结果应该怎么更改,多谢大虾!
这个是读取摸个表里所有数据,我只想读取这个表的前两条,应该怎么改?
我试着改: SqlCommand cmd = new SqlCommand("select top 2 CategoryID from Categories1", conn); 但是测试结果是不对的,请高手帮看下,为什么这样写不行,应该怎么写
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//创建SqlConnection对象
SqlConnection conn = new SqlConnection("server=(local);database=fangdawei;uid=123456;pwd=123456");
//打开数据库连接
conn.Open();
//创建SqlCommand对象
SqlCommand cmd = new SqlCommand("select * from Categories1", conn);
//接收ExecuteReader方法返回的SqlDataReader对象
SqlDataReader sdr = cmd.ExecuteReader();
//输出表格
Response.Write("<table border=1 align='center' ");
Response.Write("<tr><th>编号</th><th>图书名称</th><th>图书描述</th></tr>");
//显示学生信息
while (sdr.Read())
{
Response.Write("<tr>");
Response.Write("<td align='center'>" + sdr["CategoryID"].ToString() + "</td>");
Response.Write("<td align='center'>" + sdr["CategoryName"].ToString() + "</td>");
Response.Write("<td align='left'>" + sdr["Description"].ToString() + "</td>");
Response.Write("</tr>");
}
Response.Write("</table>");
sdr.Close();
conn.Close();
}
}