private void search()
        {
            this.lvShowdata.Items.Clear();            string line1 = "", line2 = "", line3 = "";
            if (!(cbbName1.Text == "" && txtName.Text == ""))
            {
                if (cbbName1.Text != "" && txtName.Text != "")
                {
                    if (cbbName1.Text == "产品名称")
                        line1 = "Pname";
                    else if (cbbName1.Text == "规格")
                        line1 = "Psize";
                    else if (cbbName1.Text == "价格")
                        line1 = "Pprice";
                    if (cbName.Checked == true)
                    {
                        line1 = line1 + "='" + txtName.Text + "'";
                    }
                    else if (txtName.Text != "")
                        line1 = line1 + " " + "like " + "'%" + txtName.Text + "%'";
                }
                else
                {
                    MessageBox.Show("该行输入信息不完整!请重新输入!!");
                    return;
                }
            }            if (!(cbbSize1.Text == "" && cbbSize.Text == "" && txtSize.Text == ""))
            {
                if (cbbSize1.Text != "" && cbbSize.Text != "" && txtSize.Text != "")
                {
                    if (cbbSize1.Text == "产品名称")
                        line2 = "Pname";
                    else if (cbbSize1.Text == "规格")
                        line2 = "Psize";
                    else if (cbbSize1.Text == "价格")
                        line2 = "Pprice";
                    if (cbSize.Checked == true)
                    {
                        line2 = line2 + "='" + txtSize.Text + "'";
                    }
                    else if (txtSize.Text != "")
                        line2 = line2 + " " + "like " + "'%" + txtSize.Text + "%'";
                }
                else
                {
                    MessageBox.Show("该行输入信息不完整!请重新输入!!");
                    return;
                }
            }            if (!(cbbPrice.Text == "" && cbbPrice1.Text == "" && txtPrice.Text == ""))
            {
                if (cbbPrice.Text != "" && cbbPrice1.Text != "" && txtPrice.Text != "")
                {
                    if (cbbPrice1.Text == "产品名称")
                        line3 = "Pname";
                    else if (cbbPrice1.Text == "规格")
                        line3 = "Psize";
                    else if (cbbPrice1.Text == "价格")
                        line3 = "Pprice";
                    if (cbPrice.Checked == true)
                    {
                        line3 = line3 + "='" + txtPrice.Text + "'";
                    }
                    else if (txtPrice.Text != "")
                        line3 = line3 + " " + "like " + "'%" + txtPrice.Text + "%'";
                }
                else
                {
                    MessageBox.Show("该行输入信息不完整!请重新输入!!");
                    return;
                }
            }
            string whereString = "where"+" "+line1 + " " + cbbSize.Text + " " + line2 + " " + cbbPrice.Text + " " + line3;
         
            if (line1 == "" && line2 == "" && line3 == "")
            {
                filllistview();
                return;
            }            try
            {
                DBHelper.con.Open();
                string sql = string.Format("select Pid,Pname,Pprice,Psize,Pnum from product {0}", whereString);
                SqlCommand cmd = new SqlCommand(sql, DBHelper.con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (!dr.Read())
                {
                    MessageBox.Show("查询结果为空!!");
                    return;
                }
                while (dr.Read())
                {
                    ListViewItem lvShow = new ListViewItem(dr["Pid"].ToString());
                    lvShow.SubItems.Add(dr["Pname"].ToString());
                    lvShow.SubItems.Add(dr["Pprice"].ToString());
                    lvShow.SubItems.Add(dr["Psize"].ToString());
                    lvShow.SubItems.Add(dr["Pnum"].ToString());
                    this.lvShowdata.Items.Add(lvShow);
                }
                dr.Close();
            }
            catch (Exception ex)
            {                MessageBox.Show(ex.Message); ;
            }
            finally
            {
                DBHelper.con.Close();
            }
        }把改方法放到按钮的事件中实现不了查询,请大家帮帮忙看看是什么问题,不胜感激!!

解决方案 »

  1.   

    在 string sql = string.Format("select Pid,Pname,Pprice,Psize,Pnum from product {0}", whereString); 处设置一个断点,调试一下,看看sql的值,是否能够实现查询的功能。
      

  2.   

    你page_load里写了些什么?如果你的方法能查到数据但不显示.可能是由于刷没了.
      

  3.   

    问题解决了,是
    if (!dr.Read()) 

        MessageBox.Show("查询结果为空!!"); 
         return; 

    这句的问题,这句实现不了啊,如果结果为空,我应该怎样显示("查询结果为空!!")啊?
      

  4.   

    if(dr.HasRows)
    {
    //存在数据
    }
    else
    {
    没数据
    }
      

  5.   


    可以使用HasRows来判断一下是否有数据。