OLEDB SELECT TOP 1 / MAX() 只返回 4 位数字
OLEDB SELECT TOP 1 / MAX() only returning 4 digits
伙计们,我正在使用 OLEDB with the following Command:
查询数据库
OleDbCommand maxCommand = new OleDbCommand("SELECT TOP 1 id AS maxId FROM `tableName` ORDER BY id DESC", AppConstants.OLEDBCONNECTION);
然后打印结果:
maxCommand.CommandType = CommandType.Text;
OleDbDataReader reader = maxCommand.ExecuteReader();
reader.Read();
Int64 maxId = Int64.Parse(reader["maxId"].ToString()) + 1;
问题是我已经尝试使用 MAX()
和 TOP 1
,但当我知道有超过 10000 个 id
值时,它们都返回 9999。
请帮忙,如果您需要任何其他信息,请发表评论。
您的 id
列的数据类型不是 int
。它可能是一个包含数字的 varchar
列。当您对它进行排序时,它是按词汇顺序而不是数字顺序排序的。
要获得您期望的结果,您可以将其转换为 int
(它将无法使用索引查找,因此效率不高)
select max(cast (id as int)) ...
如果可能,您应该将列类型更改为 int
。
伙计们,我正在使用 OLEDB with the following Command:
查询数据库OleDbCommand maxCommand = new OleDbCommand("SELECT TOP 1 id AS maxId FROM `tableName` ORDER BY id DESC", AppConstants.OLEDBCONNECTION);
然后打印结果:
maxCommand.CommandType = CommandType.Text;
OleDbDataReader reader = maxCommand.ExecuteReader();
reader.Read();
Int64 maxId = Int64.Parse(reader["maxId"].ToString()) + 1;
问题是我已经尝试使用 MAX()
和 TOP 1
,但当我知道有超过 10000 个 id
值时,它们都返回 9999。
请帮忙,如果您需要任何其他信息,请发表评论。
您的 id
列的数据类型不是 int
。它可能是一个包含数字的 varchar
列。当您对它进行排序时,它是按词汇顺序而不是数字顺序排序的。
要获得您期望的结果,您可以将其转换为 int
(它将无法使用索引查找,因此效率不高)
select max(cast (id as int)) ...
如果可能,您应该将列类型更改为 int
。