[英]Converting int to boolean ADO.NET SqlParameter
I am trying to use a stored procedure that returns an int. 我正在尝试使用返回int的存储过程。 Then I need to convert int to a boolean (success).
然后,我需要将int转换为布尔值(成功)。
SqlParameter Return = new SqlParameter();
Return.ParameterName = "@return";
Return.SqlDbType = SqlDbType.Int;
Return.Direction = ParameterDirection.ReturnValue;
However this line of code throws a System.InvalidCastException
error 但是,此行代码将引发
System.InvalidCastException
错误
Success = (Boolean)Command.Parameters["@return"].Value;
What should I do to not get this error? 我应该怎么做才能避免出现此错误?
Thanks 谢谢
You can use any of the following method to do what you need: 您可以使用以下任何一种方法来完成所需的操作:
Success = Convert.ToBoolean(Command.Parameters["@return"].Value);
or 要么
Success = Command.Parameters["@return"].Value==0?false:true;
or 要么
if(Command.Parameters["@return"].Value==0)
Success = false;
else
Success = true;
(they're all the same thing as Convert.ToBoolean which will be false if the value is zero, otherwise true (this came from C!) (它们与Convert.ToBoolean都相同,如果值为零,则为false,否则为true(来自C!)
EDIT: one thing I have forgotten is that, if it returns a string or an object, you might have to cast it to integer first before converting to boolean. 编辑:我已经忘记的一件事是,如果它返回字符串或对象,则可能必须先将其转换为整数,然后再转换为布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.