如果通过查询来判断呢?
比如说,我们通过一个字段查询数据库有没有这个数据,如果有就显示“有”,如果没有就显示“没有” string sql = "select * from Zhengshu where UserName like '%" + txtBox + "%'";查询会写了,但还少一个值来判断啊,高手救一下,两年没有碰.NET 了。

解决方案 »

  1.   

    放到datetable里,如果table.rows.count>0就是有
      

  2.   

    如果只是为了判断有没有,你的sql可以这么写
    string sql = "select count(ID) from Zhengshu where UserName like '%" + txtBox + "%'";
    用ExecuteScalar 返回判断
    大于0 就是有
      

  3.   

    string sql = "select count(*) from Zhengshu where UserName like '%" + txtBox + "%'";public static string login_ms(String sql)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
            //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ToString ());
            con.Open();
            SqlCommand com = new SqlCommand(sql, con);
         
            try
            {
                int isEx = Convert.ToInt32(com.ExecuteScalar());
                if (isEx > 0)
                {
                    con.Close();
                    return "有";
                }
                else
                {
                    con.Close();
                    return “木有”;
                }
            }
            catch
            {
                return “挂了”;
            }
        }
      

  4.   

     return “木有”;    这个是亮点,你这代码能编译过么?
      

  5.   


    返回到DataTable里。然后判断DataTable.Rows.Count
      

  6.   


    SELECT CASE num1 when 0 then '木有' ELSE '有' end From (select count(*) from Zhengshu where UserName like '%" + txtBox + "%'") as tb1
      

  7.   

    锅笑了,这关碰没碰.NET有什么关系,这是SQL的问题,不是任何问题都需要用业务逻辑代码来完成的,用SQL的执行效率会更高。  select 
      case when y > 0
      then '有'
      else  
      '没有'
      end  
      from (select count(1) y from Zhengshu c where c.UserName '%" + txtBox + "%'")
    )