[英]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) 如果此查询返回值,则需要返回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())
but writing this value return false, why? 但是写这个值返回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);
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. 如果使用的是ExecuteScalar()
,则将获得适当的值。
Other observation in your query 查询中的其他观察
1- USE statement is not required. 1- USE语句不是必需的。 It is only required if connection string is set to a different default catalog. 仅当连接字符串设置为其他默认目录时才需要。
2- You are using string.format
without replacing anything. 2-您正在使用string.format
而不替换任何内容。
but writing this value return false, why? 但是写这个值返回false,为什么呢?
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. 直接在SSMS中运行查询并检查输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.