简体   繁体   中英

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 ?

You can write your query like following.

 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

If you are using ExecuteScalar() , you will get the appropriate value.

Other observation in your query

1- USE statement is not required. It is only required if connection string is set to a different default catalog.

2- You are using string.format without replacing anything.

but writing this value return false, why?

It may be due to the condition are not matching with any of the record in your table. Run the query directly in SSMS and check the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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