求一条C#写的查询语句。请赐教。
一个TextBox 一个Button
在TextBox里面输入QQQ
当按下Button 后在指定的X表Y列中查询QQQ是否存在。如存在返回YES 如不存在返回NO。
请问怎么写?在线等ING....

解决方案 »

  1.   


    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())
            //存在
    }
      

  2.   

    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";
    }
      

  3.   

    SQL语句可以写成以下方式:
    select  CASE R WHEN  0 THEN 'NO' ELSE 'YES' END AS R
    FROM  
    (
    select count(*) as R from X where Y = 'aaa'
    )
    v