sql语句查询怎么判断查询结果为空?
我的代码如下
try
            {
                conn.Open();
                string sou = "select * from art where content like '%" + TextBox1.Text + "%'";
                SqlDataAdapter da = new SqlDataAdapter(sou, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds == null)
                {
                    Response.Write("没有记录!");
                }
                else
                {
                    this.sou.DataSource = ds;
                    this.sou.DataBind();
                }
            }
            catch
            {
            }if (ds == null)这里怎么写?
这里执行总是不行,为空的时候也不提示response

解决方案 »

  1.   

    一楼正解
    ds.tables[0].rows.count
      

  2.   

    你这里用DataSet 的 很好解决
    ds有 tables[]属性  然后再点属性rows.count  
      楼上以回答了 。。
      

  3.   

    我在想,都用了DataSet ds = new DataSet();
    如何还用ds == null去判断?如果Fill有表的话,是用ds.tables[0].rows.count去判断
      

  4.   

    ds是不是为null,还有就是判断ds.table.count是不是>0
      

  5.   

     if (ds.Tables[0].Count>0 )
    {}或SqlDateReader
      

  6.   

    if (ds.Tables[0].Rows.Count>0){}
      

  7.   

    如果我查询的是一个字段的怎么判断呢?
    比如
    string sou="select name from art where name like '%张三%'";
    sqlcommand cmd=new sqlcommad(sou,conn);
    string name=cmd.ExecuteScalar().tostring;
    如果查询出来没有张三这个名字,就是说值为空的话,上面这个代码会出什么样的问题?怎么解决?
    怎么判断他的直为空?
      

  8.   

    string sou="select name from art where name like '%张三%'"; 
    sqlcommand cmd=new sqlcommad(sou,conn);
    int i =cmd.ExecuteScalar(); 
    if(i=0)
    {
    messagebox.show("结果为空");
    }
    else
    {
    //你的代码
    }我很菜,可我很热心!
      

  9.   


    在你的Textbox1里面输入字符串 a' or 1=1--然后你执行下试试
      

  10.   

    gridview 等其它数据源控件 提供了数据为空的模版,,在里面写就行了.
      

  11.   

    如果怕注入,就用raplace来替换引号咯。或者用存储过程吧。
      

  12.   

    if (ds.Tables["art"].Rows.Count == 0)//art是你的表名
    {
         Page.ClientScript.RegisterStartupScript(GetType(), "sf", "<script language='javascript'>alert('没有记录').focus()</script>");
    }
    else
    {
        this.sou.DataSource = ds;
        this.sou.DataBind();
    }
      

  13.   

    //此句
    Page.ClientScript.RegisterStartupScript(GetType(), "sf", " <script language='javascript'>alert('没有记录')</script>"); 
      

  14.   

    if(ds.tables[0].rows.count==0)
    {
      //没有
    }
      

  15.   

    int i = da.Fill(ds);本身就返回填充了多少行,直接判断i即可
      

  16.   

        /// <summary>
        /// 过滤SQL语句,防止注入
        /// </summary>
        /// <param name="strSql"></param>
        /// <returns>true - 没有注入, false - 有注入 </returns>
        public bool filterSql(string sSql)
        {
            int srcLen, decLen = 0;
            sSql = sSql.ToLower().Trim();
            srcLen = sSql.Length;
            sSql = sSql.Replace("exec", "");
            sSql = sSql.Replace("master", "");
            sSql = sSql.Replace("truncate", "");
            sSql = sSql.Replace("declare", "");
            sSql = sSql.Replace("create", "");
            sSql = sSql.Replace("xp_", "");
            decLen = sSql.Length;
            return (srcLen == decLen);
        }
      

  17.   

     public static DataTable GetTable(DataSet ds)
            {
                DataTable dt;
                if (ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        dt = ds.Tables[0];
                    }
                    else
                    {
                        dt = null;
                    }
                }
                else
                {
                    dt = null;
                }
                return dt;
            }
      

  18.   

    本身已经使用了DataSet ds = new DataSet();
    那不管查询出来有没有数据,ds不会等于null吧
      

  19.   

    if(ds.tables[0].rows.count==0)
    {
      //no records
    }
      

  20.   

    一群傻逼,人家问sql中怎么判断,没看见题目就答题