繁体   English   中英

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

[英]query in c# returns false after getting values from the db

如果此查询返回值,则需要返回true(并且它在db中具有值)

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

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

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

 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中运行查询并检查输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM