如何从DataTable的最后一行检索值?

时间:2023-02-08 13:20:05

I am having problems retrieving values from the last inserted row in a Data-table. I have a login form and the values will be inserted in the table has the ID (int,an auto incremented value), userID (int), logintime (smalldatetime) and logouttime(smalldatetime). The code which is used to inside the login button,it thus inserts all values except the log out time

我在检索数据表中最后一个插入行的值时遇到问题。我有一个登录表单,并且将在表中插入的值包含ID(int,自动递增值),userID(int),logintime(smalldatetime)和logouttime(smalldatetime)。用于登录按钮内部的代码,因此插入除退出时间之外的所有值

DateTime t1 = DateTime.Now;
objbal2.insertLoginTime(s, t1);

and in the log out button I am updating the table, so I have to retrieve the last row values. I am updating the table with reference to the ID value and userID. Can I use this query get the values? But I can't figure out how?

在注销按钮中我正在更新表,所以我必须检索最后一行值。我正在参考ID值和userID更新表。我可以使用此查询获取值吗?但我无法弄清楚如何?

SELECT COLUMN FROM TABLE ORDER BY COLUMN DESC

Thanks in advance

提前致谢

4 个解决方案

#1


24  

if you have to read the values from last row then

如果你必须从最后一行读取值

DataRow lastRow = yourTable.Rows[yourTable.Rows.Count-1];

will return you last row. and you can read the values from it.

将在最后一行返回。你可以从中读取值。

My second guess is that by datatable you are referring to table in sql server.

我的第二个猜测是,通过datatable,你指的是sql server中的表。

Then with small modification your query is fine as well.

然后通过小修改你的查询也很好。

SELECT TOP 1 COLUMN FROM TABLE ORDER BY COLUMN DESC

#2


6  

var dt = new DataTable();
dt.AsEnumerable().Last();

dt.AsEnumerable() returns an IEnumerable<DataRow>

dt.AsEnumerable()返回一个IEnumerable

#3


1  

You can got the value from your session and then update your database as per your change

您可以从会话中获取值,然后根据您的更改更新数据库

DateTime t2 = DateTime.Now;

DataRow Row = Mylagin_dataTable.Rows.Count-1

Row[0]["LogoutTime"] = t2 ;

if your ID in session then use below

如果你的会话ID在下面使用

DateTime t2 = DateTime.Now;

DataRow Row = Mylagin_dataTable.Select("LoginID='"+ HttpContext.Current.Session["loginID"] + "'");

Row[0]["LogoutTime"] = t2 ;

#4


0  

Execute following query on Application Exit event, this will do your work

在Application Exit事件上执行以下查询,这将完成您的工作

string _query = string.Format("update top 1 SessionTable set LogOutTime = {0} where UserID = {1} order by ID desc", DateTime.Now, UserID);

string _query = string.Format(“update top 1 SessionTable set LogOutTime = {0}其中UserID = {1} order by ID desc”,DateTime.Now,UserID);

#1


24  

if you have to read the values from last row then

如果你必须从最后一行读取值

DataRow lastRow = yourTable.Rows[yourTable.Rows.Count-1];

will return you last row. and you can read the values from it.

将在最后一行返回。你可以从中读取值。

My second guess is that by datatable you are referring to table in sql server.

我的第二个猜测是,通过datatable,你指的是sql server中的表。

Then with small modification your query is fine as well.

然后通过小修改你的查询也很好。

SELECT TOP 1 COLUMN FROM TABLE ORDER BY COLUMN DESC

#2


6  

var dt = new DataTable();
dt.AsEnumerable().Last();

dt.AsEnumerable() returns an IEnumerable<DataRow>

dt.AsEnumerable()返回一个IEnumerable

#3


1  

You can got the value from your session and then update your database as per your change

您可以从会话中获取值,然后根据您的更改更新数据库

DateTime t2 = DateTime.Now;

DataRow Row = Mylagin_dataTable.Rows.Count-1

Row[0]["LogoutTime"] = t2 ;

if your ID in session then use below

如果你的会话ID在下面使用

DateTime t2 = DateTime.Now;

DataRow Row = Mylagin_dataTable.Select("LoginID='"+ HttpContext.Current.Session["loginID"] + "'");

Row[0]["LogoutTime"] = t2 ;

#4


0  

Execute following query on Application Exit event, this will do your work

在Application Exit事件上执行以下查询,这将完成您的工作

string _query = string.Format("update top 1 SessionTable set LogOutTime = {0} where UserID = {1} order by ID desc", DateTime.Now, UserID);

string _query = string.Format(“update top 1 SessionTable set LogOutTime = {0}其中UserID = {1} order by ID desc”,DateTime.Now,UserID);