获取SQL表中的最高ID号LINQ,C#? [英] Get the highest ID number in a SQL table , LINQ , C#?
本文介绍了获取SQL表中的最高ID号LINQ,C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在空的SQL表中获得最高的ID号?
例如:
假设我们有一个包含十行的表,但是所有行都被删除了,现在我们要获取最高行的ID !!!
How can we get the highest ID number in an empty SQL table ?
e.g:
Assume we have a table with ten rows, but all of rows were deleted , now we wanna get the highest row's ID !!!
以下代码由于表为空而引发异常:
The below code throw an Exception because the table is empty :
var maxID = db.TableNames.Select(q => q.Id).Max();
推荐答案
For SQL Server try the IDENT_CURRENT function.
请注意,由于交易失败等原因,标识值序列中可能存在间隙,因此无法保证使用该值提交过行.
Note that there can be gaps in identity value sequences because of failed transactions etc, so there is no guarantee that a row was ever committed with this value.
这篇关于获取SQL表中的最高ID号LINQ,C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文