例如,我有表列ID
所见如下:如何从SQL Server 2012中的表列中查询最高值?
ID
------
1
2
3
4
我怎样才能查询,所以我可以得到4?
我使用的是SQL Server 2012的
例如,我有表列ID
所见如下:如何从SQL Server 2012中的表列中查询最高值?
ID
------
1
2
3
4
我怎样才能查询,所以我可以得到4?
我使用的是SQL Server 2012的
select max(ID) from [Table]
您应该使用SELECT max(Id) FROM mytable
,你应该能够完成,使用这样的代码:
int maxId = -1;
string connectionString = "yourConnectionString";
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand command = new SqlCommand("SELECT max(Id) FROM mytable", con))
{
maxId = Convert.ToInt32(command.ExecuteScalar());
}
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
您也可以使用
SELECT TOP 1 ID
FROM mytable
ORDER BY ID DESC
要放弃计算,并利用排序功能找到您检查的任何列中的最高值。
感谢,
C§