从数据库获取值后,C#中的查询返回false

时间:2019-01-27 10:12:28

标签: c# mysql sql-server

如果此查询返回值(并且它在数据库中有值),我需要返回true

 SELECT * from AllMembers a 
    join Cards c on c.IDMember = a.MemberID
    where LEN(a.EmployeeNum) = 9
    and LEFT(c.CardNumber,3) = '777'
    and a.Email is not null
    and a.LastUpdate > DATEADD(YEAR, -1 , GETDATE())
    and c.CardStatus = 1
    and c.AddedTime  > DATEADD(YEAR, -1 , GETDATE())
    or 
     LEN(a.EmployeeNum) = 9
    and LEFT(c.CardNumber,4) = '2010'
    and a.Email is not null
    and a.LastUpdate > DATEADD(YEAR, -1 , GETDATE())
    and c.CardStatus = 1
    and c.AddedTime  > DATEADD(YEAR, -1 , GETDATE())

但是写这个值返回false,为什么?

 string Query= string.Format(@"use Knowledge4All 
                                    IF EXISTS (
                                                SELECT ID FROM {0}..AllMembers 
                                                JOIN Cards where  IDMember = @memberID
                                                and (where LEN(EmployeeNum) = 9
                                                and LEFT(CardNumber,3) = '777'
                                                and Email is not null
                                                and LastUpdate > DATEADD(YEAR, -1 , GETDATE())
                                                and CardStatus = 1
                                                and AddedTime  > DATEADD(YEAR, -1 , GETDATE())))
                                                 select 1
                                                 OR
                                                 IF exists(
                                                (LEN(EmployeeNum) = 9
                                                and LEFT(CardNumber,4) = '2010'
                                                and Email is not null
                                                and LastUpdate > DATEADD(YEAR, -1 , GETDATE())
                                                and CardStatus = 1
                                                and AddedTime  > DATEADD(YEAR, -1 , GETDATE()) )
                                                SELECT 1", DbName);

我写错了吗?为什么要朗诵这个最好?

1 个答案:

答案 0 :(得分:0)

您可以像下面这样编写查询。

 IF EXISTS(SELECT 1 from AllMembers a 
    join Cards c on c.IDMember = a.MemberID
    where LEN(a.EmployeeNum) = 9
    and LEFT(c.CardNumber,3) = '777'
    and a.Email is not null
    and a.LastUpdate > DATEADD(YEAR, -1 , GETDATE())
    and c.CardStatus = 1
    and c.AddedTime  > DATEADD(YEAR, -1 , GETDATE())
    or 
     LEN(a.EmployeeNum) = 9
    and LEFT(c.CardNumber,4) = '2010'
    and a.Email is not null
    and a.LastUpdate > DATEADD(YEAR, -1 , GETDATE())
    and c.CardStatus = 1
    and c.AddedTime  > DATEADD(YEAR, -1 , GETDATE())
    )
    BEGIN
     SELECT 1 as Status
    END
    ELSE
    BEGIN
     SELECT 0 AS Status
    END

如果您使用的是ExecuteScalar(),则会获得适当的值。

查询中的其他观察结果

1-不需要USE语句。仅在将连接字符串设置为其他默认目录时才需要。

2-您正在使用string.format而不替换任何东西。

  

但是写这个值返回false,为什么?

这可能是由于条件与表中的任何记录都不匹配。直接在SSMS中运行查询并检查输出。