如何获取asp.net中数据集表中的最后一条记录 [英] How to get last record in dataset table in asp.net
问题描述
大家好,
如何在asp.net&中获取数据集表中的最后一条记录我希望以字符串形式返回该记录。
请让我知道该怎么办。
[从OP更新]
我提供的内容我不是初学者,我尝试用下面的代码作为你的提示,但它返回所有thr记录,这里是代码
SqlConnection oCon = new SqlConnection( @ Initial Catalog = master; server = 192.168.1.29\SQLEXPRESS; user id = sa; password = Mobile123);
受保护 void Page_Load( object sender,EventArgs e){}
protected DataRow getLastRec()
{
SqlDataAdapter oDa = new SqlDataAdapter( select * from Table_Test,oCon);
DataSet ds = new DataSet();
oDa.Fill(ds);
DataRow dr =(DataRow)ds.Tables [ 0 ]。行[ds.Tables [ 0 ]。Rows.Count -1];
return dr;
}
受保护 void btnGet_Click( object sender,EventArgs e)
{
GridView1.DataSource = getLastRec(); GridView1.DataBind();
}
您唯一需要做的就是将查询更改为:
通过id desc从Table_Test订单中选择前1名id,col2,col3,col4,coln
通过这样做,你将只获得1数据集中的行。这一行将是表中最后一行。
在查询中,'id'是主键列,你必须写出你想要数据的所有列名 - 在上面的查询中,col2,col3,... coln就是那些列。
这样做就完成了!
String pid;
ProductTableAdapter.Fill(MaccDataset);
DataView dv = new DataView(MaccDataset.Tables [ 0 ]);
pid = dv.Table.Rows [dv.Table.Rows.Count - 1 ] [ 0 ]。ToString();
获取最后一行DataSet.Tables(0).Rows(DataTable.Tables( 0).Rows.Count -1)
使用 DataRow.Item [ ^ ]
Hi All,
How to get last record in dataset table in asp.net & that to i want to return that record in string.
please let me know the way how to do.
[Update from OP]
What you provide i m not get as am beginner i tried with below code as your hint but it returns all thr records,here is the code
SqlConnection oCon = new SqlConnection(@"Initial Catalog=master;server=192.168.1.29\SQLEXPRESS;user id=sa;password=Mobile123");
protected void Page_Load(object sender, EventArgs e) { }
protected DataRow getLastRec()
{
SqlDataAdapter oDa = new SqlDataAdapter("select * from Table_Test", oCon);
DataSet ds = new DataSet();
oDa.Fill(ds);
DataRow dr = (DataRow)ds.Tables[0].Rows[ds.Tables[0].Rows.Count -1];
return dr;
}
protected void btnGet_Click(object sender, EventArgs e)
{
GridView1.DataSource = getLastRec(); GridView1.DataBind();
}
The Only thing you have to do is Change the query to:
"select top 1 id, col2, col3, col4, coln from Table_Test order by id desc"
By doing this, you will get only 1 row in the dataset. This row will be the last one present in your table.
In the query, 'id' is the Primary Key column and you have to write all column names from which you want data - In the above query, col2,col3,...coln are those columns.
Do this and you're done!
String pid; ProductTableAdapter.Fill(MaccDataset); DataView dv = new DataView(MaccDataset.Tables[0]); pid = dv.Table.Rows[dv.Table.Rows.Count - 1][0].ToString();
Get the last row withDataSet.Tables(0).Rows(DataTable.Tables(0).Rows.Count -1)
Get the values using DataRow.Item[^]
这篇关于如何获取asp.net中数据集表中的最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!