获取数据库最后一行显示 [英] Get database last row displayed

查看:88
本文介绍了获取数据库最后一行显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个名为sample的数据库,我在gridview中显示了那些示例数据库记录。它的工作。



我的任务是什么,我必须显示从示例数据库到gridview的所有行的最后一个数据库记录。



例如,一个名为database_tbl的表存在2个字段,即名称和年龄。



我将使用gridview显示这些记录sqldataadapter,ds()等,假设名为abcd的记录是该表中的最后一个数据库记录,我需要在该特定网格视图的所有行中显示abcd。



谢谢

Hello,

I have a database named "sample", I displayed those sample database records in gridview. Its working.

What my task is, I have to show the last database record from sample database to all the rows of gridview.

For eg., a table named database_tbl present with 2 fields i.e., name and age.

I will display those records with gridview using sqldataadapter, ds() etc, suppose a record named "abcd" is the last database record in that table, I need that "abcd" to be displayed in all the rows of that particular gridview.

Thank You

推荐答案

我的理解是你要在新列中显示最后一条记录的名称以及表格中的其他记录。如果这是要求,请检查以下示例

My understanding is you want to show the name of the last record in a new column along with other records in your table. If this is the requirement check the below sample
DECLARE @database_tbl  TABLE
(
    ID INT IDENTITY(1,1),
    Name VARCHAR(100),
    Age INT
)

INSERT INTO @database_tbl
SELECT 'abc1', 12 UNION ALL
SELECT 'abc2', 13 UNION ALL
SELECT 'abc3', 18 UNION ALL
SELECT 'abcd', 21


DECLARE @LastRecordName VARCHAR(100)
SELECT TOP 1 @LastRecordName = Name FROM @database_tbl ORDER BY ID DESC

SELECT *, @LastRecordName AS LastRecordName FROM @database_tbl





这里我们将最后一条记录的名称存储在变量中,并在最终查询中使用该变量。



Here we store the name of the last record in a variable and use that variable in the final query.


select [ID], [Name], [Age] from YourTable 
except 
select Top 1 [ID], [Name], [Age] from YourTable order by ID DESC
union all
select Top 1 [ID], [Name], [Age] from YourTable order by ID DESC



此行


This line

select [ID], [Name], [Age] from YourTable 
except 
select Top 1 [ID], [Name], [Age] from YourTable order by ID DESC

将过滤最后一行并使用此行再次添加

will filter the last row and again add it using this line

select Top 1 [ID], [Name], [Age] from YourTable order by ID DESC

。排除它然后包括的原因在最后一页发挥作用。如果不以这种方式完成,它将在最后一页上两次添加最后一条记录。

. The reason to exclude it and then including comes into play for the last page. If it is not done in this way, It will add the last record twice on the last page.


这篇关于获取数据库最后一行显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆