MAX(id) using SqlDataReader C#

那年仲夏 提交于 2019-11-29 08:04:28
BrokenGlass

When you select the maximum id you shouldn't use a SqlDataReader - the query returns just one item, which by default is unnamed so your existing query breaks because it expects a result named "FormID" - although you could have "fixed" your query by using "SELECT MAX(FormID) as FormId FROM tbl_Form". Instead use ExecuteScalar():

myCommand.CommandText = "SELECT MAX(FormID) FROM tbl_Form";
int maxId = Convert.ToInt32(myCommand.ExecuteScalar());

It's been a while since I used this style of database access, but I think you just need

select max(FormID) from tbl_Form

along with a call to ExecuteScalar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!