当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Converting int to boolean ADO.NET SqlParameter

问题描述:

I am trying to use a stored procedure that returns an int. Then I need to convert int to a boolean (success).

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

 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!)

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: