在c#中的GridView中显示最新的10条数据库记录
Display latest 10 database records in GridView in c#
我想在 C# 的 GridView 中显示最新的 10 个数据库条目。现在我正在使用此代码显示从旧到新的条目。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["jason"].ConnectionString;
string query = "SELECT TOP 10 did,name,mobile from dealer;";
query += "SELECT TOP 10 CUSTREGNO,DATEOFCOM,PLANANDTERM from customerreg";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
gvCustomers.DataSource = ds.Tables[0];
gvCustomers.DataBind();
gvEmployees.DataSource = ds.Tables[1];
gvEmployees.DataBind();
}
}
}
}
}
}
}
我认为如果你有一个自增主键会更容易。然后您可以从 "SELECT TOP 10 * FROM yourTable ORDER BY YourPrimaryKey DESC"
中检索这些值
我想在 C# 的 GridView 中显示最新的 10 个数据库条目。现在我正在使用此代码显示从旧到新的条目。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["jason"].ConnectionString;
string query = "SELECT TOP 10 did,name,mobile from dealer;";
query += "SELECT TOP 10 CUSTREGNO,DATEOFCOM,PLANANDTERM from customerreg";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
gvCustomers.DataSource = ds.Tables[0];
gvCustomers.DataBind();
gvEmployees.DataSource = ds.Tables[1];
gvEmployees.DataBind();
}
}
}
}
}
}
}
我认为如果你有一个自增主键会更容易。然后您可以从 "SELECT TOP 10 * FROM yourTable ORDER BY YourPrimaryKey DESC"
中检索这些值