using (SqlConnection conn = new SqlConnection("连接字符串")) { conn.Open(); SqlCommand cmd = new SqlCommand("select * from X where Y ='QQQ'",conn); SqlDataReader sdr = cmd.ExecuteReader(); if(sdr.Read()) //存在 }
using (SqlConnection conn = new SqlConnection("连接字符串")) { conn.Open(); SqlCommand cmd = new SqlCommand("select count(Y) from X where Y ='QQQ'",conn); return cmd.ExecuteScalar().ToString()!="0"; }
SQL语句可以写成以下方式: select CASE R WHEN 0 THEN 'NO' ELSE 'YES' END AS R FROM ( select count(*) as R from X where Y = 'aaa' ) v
using (SqlConnection conn = new SqlConnection("连接字符串"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from X where Y ='QQQ'",conn);
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.Read())
//存在
}
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(Y) from X where Y ='QQQ'",conn);
return cmd.ExecuteScalar().ToString()!="0";
}
select CASE R WHEN 0 THEN 'NO' ELSE 'YES' END AS R
FROM
(
select count(*) as R from X where Y = 'aaa'
)
v