我想从数据库中获取第一条记录该怎么做 [英] i want to fetch 1st record from database how can do that

查看:144
本文介绍了我想从数据库中获取第一条记录该怎么做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string query1st = "SELECT * FROM Registration_Master ";
                     com = new SqlCommand(query1st, con);
                     com.CommandType = CommandType.Text;
                     SqlDataAdapter da = new SqlDataAdapter(com);
                     DataTable dt1st = new DataTable();
                     da.Fill(dt1st);
                                          
                     if (dt1st.Rows.Count>0)
                     {


                     }


但这给了我错误的输出

添加了代码块[/编辑]


but this gives me wrong output

Code block added[/Edit]

推荐答案

TSQL
TSQL
SELECT TOP 1 * FROM Registration_Master


ORACLE( [


ORACLE (source[^])
If you want just a first selected row you can:

select fname from MyTbl where rownum = 1


您还可以使用解析函数对x顶部的x进行排序和取值


you can also use analytic functions to order and take the top x

select max(fname) over (rank() order by some_factor) from MyTbl


MySQL


MySQL

SELECT * FROM Registration_Master LIMIT 1;



但是,如果您想要最新的记录,则在每种情况下都必须根据主键降序(或可以排序以提供最新记录的其他列)对记录进行排序



If you want the latest record however, in each case you will have to order your records based on your primary key descending(or other column that can be sorted to provide the latest record)


这篇关于我想从数据库中获取第一条记录该怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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