我查询了数据库中的一个表,想实现如果表中有数据则执行一些操作,没数据则不执行,代码如下
SqlConnection cona = new SqlConnection("server=.;database=superet;Trusted_Connection=True");
            SqlCommand cmda = new SqlCommand();
            cona.Open();
            cmda.Connection = cona;
            string stra = "select *  from sell where 销售日期 between '" + this.dateTimePicker1.Text.ToString() + "' and '" + this.dateTimePicker2.Text.ToString() + "'";
            SqlDataAdapter adaa = new SqlDataAdapter(stra, cona);                      
            DataSet dsa = new DataSet();
            adaa.Fill(dsa);            
            cona.Close();
            if (dsa != null)
            {
                comboBox1.Items.Clear();
                SqlConnection cona1 = new SqlConnection("server=.;database=superet;Trusted_Connection=True");
                SqlCommand cmda1 = new SqlCommand();
                cona1.Open();
                cmda1.Connection = cona1;
                string stra1 = "select Sum(货物数量)  from sell where 销售日期 between '" + this.dateTimePicker1.Text.ToString() + "' and '" + this.dateTimePicker2.Text.ToString() + "'";
                SqlDataAdapter adaa1 = new SqlDataAdapter(stra1, cona1);
                DataTable dta1 = new DataTable();                
                adaa1.Fill(dta1);
                cona1.Close();
                this.button1.Enabled = true;
                SumNum = Convert.ToInt32(dta1.Rows[0][0].ToString());                SqlConnection cona11 = new SqlConnection("server=.;database=superet;Trusted_Connection=True");
                SqlCommand cmda11 = new SqlCommand();
                cona11.Open();
                cmda11.Connection = cona11;
                string stra11 = "select distinct (cast(货物名称 as varchar(8000))) from sell where 销售日期 between '" + this.dateTimePicker1.Text.ToString() + "' and '" + this.dateTimePicker2.Text.ToString() + "'";
                SqlDataAdapter adaa11 = new SqlDataAdapter(stra11, cona11);
                DataTable dta11=new DataTable();
               
                adaa11.Fill(dta11);
                cona11.Close();
                for (int i = 0; i < dta11.Rows.Count; i++)
                {
                    comboBox1.Items.Add(dta11.Rows[i][0]);
                }            
            } 
但是没有数据时还是会执行if里的语句,怎么让他没数据的时候跳过去?                     

解决方案 »

  1.   

    将“if (dsa != null)”替换为“ if(DataSet.Tables[?].Rows.Count==0)”试试
      

  2.   

    正解
    我做了个测试
      DataSet ds = new DataSet();
                    if (ds == null)
                    {
                        Response.Write("Yes it's Null");
                    }
                    else
                    {
                        Response.Write("No it's not Null");
                    }
    返回为 No it's not Null 说明当你 DataSet dsa = new DataSet();后dsa 就不可以是NULL了
    用上面仁兄的办法吧
      

  3.   

     if (dsa != null&& dsa .Tables[?].Rows.Count==0)
      

  4.   

    應該先判斷Dataset是否為空的,用Dataset.Table[0].Rows.Count來判斷!
      

  5.   

    if (dsa != null&& dsa .Tables[?].Rows.Count!=0)