我要查询出面积位于两者之间的数据,数据库中的面积存储的是例如99.8,120.6,110.5之类的数据,代码如下
string sql = "select * from 出售房 where ";
if (checkarea.Checked == true)
            {
                if (txtarea1.Text == "") txtarea1.Text = "0";
                if (txtarea2.Text == "") txtarea2.Text = "100000";               
                if (strwhere != string.Empty)
                {
                    strwhere += "and 建筑面积 between'" + txtarea1.Text.Trim().ToString() + "' and '" + txtarea2.Text.Trim().ToString() + "'";
                }
                else
                {
                    strwhere += "建筑面积 between'" + txtarea1.Text.Trim().ToString() + "' and '" + txtarea2.Text.Trim().ToString() + "'";
                }
            }
sql = sql + strwhere;
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            this.dataGridView1.DataSource = dt.DefaultView;
问题是现在怎么就是找不出来呢?

解决方案 »

  1.   

    你把最后的sql变量内容显示出来看看就知道了:
    MessageBox.show(sql )
      

  2.   

    一般是你的判断不严密,导致有时动态生成的sql语句有问题,所以最简单的方法是,在执行这个sql语句前,看看它是否正确。
      

  3.   

    string sql = "select * from 出售房 where ";
    if(checkarea.Checked)
    {
      sql +=string.IsNullOrEmpty(txtarea1.Text)? "建筑面积 between '0'":"建筑面积 between  between '" + txtarea1.Text.Trim().ToString() + "'";
    }
    单步跟踪查看sql
      

  4.   

    between之间的那个分号应该是英文的分号吧
      

  5.   

    单步跟踪显示sql为"select * from 出售房 where 建筑面积 between'90' and '120'",没有问题啊
    我两个文本框中输入的是90和120
      

  6.   

    if (checkfloor.Checked == true)
                {
                    if (txtfloor1.Text == "") txtfloor1.Text = "0";
                    if (txtfloor2.Text == "") txtfloor2.Text = "1000";
                    if (strwhere != string.Empty)
                    {
                        strwhere += "and 楼层 between'" + txtfloor1.Text.Trim().ToString() + "' and '" + txtfloor2.Text.Trim().ToString() + "'";
                    }
                    else
                    {
                        strwhere += "楼层 between'" + txtfloor1.Text.Trim().ToString() + "' and '" + txtfloor2.Text.Trim().ToString() + "'";
                    }
                }
    而当我按照楼层查询时,查询2~5之间的信息确查出来了,但是当我只输入两个文本框中的第一个,而第二个不输入,如第一个输入4,第二个文本框则不输入,此时就查询不出来了,奇怪啊
      

  7.   

    第二个不输入的时候,你的sql还能运行?再跟综呢?
      

  8.   

    能运行,第二个不输入时单步跟踪的sql为"select * from 出售房 where 楼层 between'2' and '1000'"