What data type does the SQLCommand method ExecuteScalar() return?

时光怂恿深爱的人放手 提交于 2019-12-01 18:18:18

SCOPE_IDENTITY() returns a decimal in code, otherwise known as NUMERIC(38,0) in TSQL.

http://msdn.microsoft.com/en-us/library/ms190315.aspx

So if you want a direct cast, you can do (int)(decimal)cmd.ExecuteScalar();. Note that a decimal to int conversion can lose information in the strictest sense, so just be advised. But with your identity column being an integer, the conversion will be safe.

It's probably returning a boxed instance of a different numeric type, such as long.
A boxed long cannot be converted to int.

You can call GetType() on the return value to see what type it really is.

From the end of your INSERT statement

SELECT SCOPE_IDENTITY()

It's returning the identity value of the row inserted into the [Users] table.

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