C# 将SQL Server中的SELECT中的布尔值转换为C中的布尔值?
我的选择中有此代码:C# 将SQL Server中的SELECT中的布尔值转换为C中的布尔值?,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我的选择中有此代码: SELECT A.CompletedDate, CASE WHEN (@AdminTestId IS NULL AND @UserTestId IS NULL) THEN 0 WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId = A.UserTestId) THEN 1 WHEN (@AdminTestId = temp.AdminTestId AND @Us
SELECT A.CompletedDate,
CASE
WHEN (@AdminTestId IS NULL AND @UserTestId IS NULL) THEN 0
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId = A.UserTestId) THEN 1
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId IS NULL) THEN 1
ELSE 0
END AS [Current],
在我的ASP.NET DTO中,我有:
public partial class GetTestsDTO
{
public bool? GradePass { get; set; }
// the following line is what is giving the error. If I remove
// the line it will not try to convert the data and there is no
// error. Note that I want to put this into a bool as web.api
// then passes it to the client as a bool
public bool Current { get; set; }
public DateTime? CompletedDate { get; set; }
}
这给了我一个错误,我希望能得到一些帮助
错误是:
message=从物化的“System.Int32”类型到的指定强制转换
“System.Boolean”类型无效
如注释中所述,您将返回一个整数。您需要返回一个位,ASP.NET将其理解为布尔值
SELECT A.CompletedDate,
CASE
WHEN (@AdminTestId IS NULL AND @UserTestId IS NULL) THEN
CONVERT(bit, 0)
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId = A.UserTestId) THEN
CONVERT(bit, 1)
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId IS NULL) THEN
CONVERT(bit, 1)
ELSE
CONVERT(bit, 0)
END AS [Current],
或者正如下面的注释所指出的,您可以将整个CASE语句包装在CONVERT中
您还可以分别使用CAST1作为位和CAST0作为位来替换CONVERTbit、1和CONVERTbit、0。有关详细信息,请参阅
您还可以在转换客户端执行以下操作:
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
var dto = new GetTestsDTO();
dto.Current = Convert.ToBoolean(reader.GetInt32(1));
}
}
正如前面的注释所示,SQL不能返回布尔值,您也不能返回它 我的建议/解决方案是,在我工作的每一个地方,我都会使用,就是简单地恢复对象中的整数,然后使用“映射”函数将该整数转换为布尔值
SELECT A.CompletedDate,
CASE
WHEN (@AdminTestId IS NULL AND @UserTestId IS NULL) THEN
CONVERT(bit, 0)
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId = A.UserTestId) THEN
CONVERT(bit, 1)
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId IS NULL) THEN
CONVERT(bit, 1)
ELSE
CONVERT(bit, 0)
END AS [Current],
简单的方法可以做到这一点;Java版本预告:
public static boolean integerToBoolean(Integer myInt){
return myInt == 1 ? true : false;
}
祝您好运。SQL Server端最简单的方法是将值0和1转换为数据类型:
SELECT A.CompletedDate,
CASE
WHEN (@AdminTestId IS NULL AND @UserTestId IS NULL)
THEN CAST(0 AS BIT)
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId = A.UserTestId)
THEN CAST(1 AS BIT)
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId IS NULL)
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT)
END AS [Current],
或同时使用整个表达式:
SELECT A.CompletedDate,
CAST((CASE
WHEN (@AdminTestId IS NULL AND @UserTestId IS NULL) THEN 0
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId = A.UserTestId) THEN 1
WHEN (@AdminTestId = temp.AdminTestId AND @UserTestId IS NULL) THEN 1
ELSE 0
END)
AS BIT) AS [Current],
:
我试着像'true'/'false'那样返回布尔值,它也起了作用。
环境:C-整洁
SQL:-SQL Server 2012
示例:-当@AdminTestId为NULL且@UserTestId为NULL时,则为“false”;当@AdminTestId=temp.AdminTestId且@UserTestId=A.UserTestId时,则为“true”
这两种情况都适用于“真”或“真”或“假”或“假”
因此,除了公认的答案(这是一个有效的答案)之外,这对我来说也是有效的,是的,这可能也是由于Dapper的原因,如果它在普通ADO.NET中不起作用可能是因为您返回的是一个整数?将0转换为位,将1转换为位?SQL Server没有公开的布尔数据类型,所以你没有在选择中返回一个。正如lad所建议的,它确实有一点数据类型,并且ADO.Net支持将这些数据类型转换为布尔值。@lad2025-我尝试了你的建议,效果很好。如果你想补充这一点,我可以接受。感谢可能需要在else语句中为0设置convert,因为0也是一个整数,而不是布尔值。Done。我一保存答案就注意到了!或者只做一次转换,在案例外部而不是每个案例内部。或者只使用
╔═════════════════════════════════╦═════════════════════╦═══════════════════════╗
║ SQL Server Database Engine type ║ .NET Framework type ║ SqlDbType enumeration ║
╠═════════════════════════════════╬═════════════════════╬═══════════════════════╣
║ bit ║ Boolean ║ Bit ║
╚═════════════════════════════════╩═════════════════════╩═══════════════════════╝
I may not be an authority to answer it, but below too works for me :-