c# - 如何使用执行标量返回 bool 值

标签 c# asp.net sql-server visual-studio-2010 sql-server-2008

我有一个返回 bool 变量的存储过程:

这是我的 C# 代码,当我尝试返回一个 int 变量时它可以工作。但是当我尝试返回 bool 时它不起作用。

DbCommand dbCommand = db.GetStoredProcCommand("spGetConfigureAlerts");
            object o = db.ExecuteScalar(dbCommand);
            bool item = o == null ? 0 : (bool)o;
            return item;

我有它,如上所示,但在第三行它说:

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'bool'    

我该如何解决?

最佳答案

如果要初始化一个 bool 变量,为什么要使用 0

bool item = (o == null || DBNull.Value == o) ? false : (bool)o;

关于c# - 如何使用执行标量返回 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18796648/

相关文章:

c# - 使用ajax在iframe中加载内容

c# - MonoTouch - 重用 UIAlertVIew?

c# - double 格式 - 显示零位或两位小数

c# - 为什么我不能使用 Global.asax.cs 中 Models 文件夹中的类?

sql - 在普通 sql 中对行进行分组

c# - 我如何订购 IEnumerable<T> 以使特殊项目始终位于底部?

c# - 在运行时生成报告

c# - ASP :Button inside asp:hyperlink does not navigate to page in internet explorer

php - 使用 PHP (Windows/Linux) 连接到 SQL 2008 服务器

sql - insert into.. select 语句是事务性的吗?