如何转换"0"和"1"表示虚假和真实 [英] How to convert "0" and "1" to false and true

查看:104
本文介绍了如何转换"0"和"1"表示虚假和真实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过Odbc连接到数据库的方法.我的存储过程 调用具有一个返回值,该返回值从数据库侧为"Char".现在我正在抓 以字符串形式返回值,并在简单的if语句中使用它.我真的不喜欢这个主意 当只能从数据库返回两个值(0和1)时比较这样的字符串的方法.

I have a method which is connecting to a database via Odbc. The stored procedure which I'm calling has a return value which from the database side is a 'Char'. Right now I'm grabbing that return value as a string and using it in a simple if statement. I really don't like the idea of comparing a string like this when only two values can come back from the database, 0 and 1.

OdbcCommand fetchCommand = new OdbcCommand(storedProc, conn);

fetchCommand.CommandType = CommandType.StoredProcedure;
fetchCommand.Parameters.AddWithValue("@column ", myCustomParameter);
fetchCommand.Parameters.Add("@myReturnValue", OdbcType.Char, 1)
            .Direction = ParameterDirection.Output;
fetchCommand.ExecuteNonQuery();

string returnValue = fetchCommand.Parameters["@myReturnValue"].Value.ToString();
if (returnValue == "1")
{
    return true;
} 

处理这种情况的正确方法是什么?我已经尝试过'Convert.ToBoolean()'似乎 就像显而易见的答案一样,但是我遇到了'字符串未被识别为有效布尔值的情况. '抛出异常.我错过了什么吗 在这里,还是有另一种方式使"1"和"0"像真假一样起作用?

What would be the proper way to handle this situation. I've tried 'Convert.ToBoolean()' which seemed like the obvious answer but I ran into the 'String was not recognized as a valid Boolean. ' exception being thrown. Am I missing something here, or is there another way to make '1' and '0' act like true and false?

谢谢!

推荐答案

怎么样:

return (returnValue == "1");

或如下建议:

return (returnValue != "0");

正确的选择取决于成功的结果.

The correct one will depend on what you are looking for as a success result.

这篇关于如何转换"0"和"1"表示虚假和真实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆