首页 > 解决方案 > query in c# returns false after getting values from the db

问题描述

I need to return true if this query returns value, (and it has values in the 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())

but writing this value return false, why?

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

Am I writing this wrong? what is the best why to rum this ?

标签: c#mysqlsql-server

解决方案


您可以编写如下查询。

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


推荐阅读