我要查询出面积位于两者之间的数据,数据库中的面积存储的是例如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;
问题是现在怎么就是找不出来呢?
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;
问题是现在怎么就是找不出来呢?
MessageBox.show(sql )
if(checkarea.Checked)
{
sql +=string.IsNullOrEmpty(txtarea1.Text)? "建筑面积 between '0'":"建筑面积 between between '" + txtarea1.Text.Trim().ToString() + "'";
}
单步跟踪查看sql
我两个文本框中输入的是90和120
{
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,第二个文本框则不输入,此时就查询不出来了,奇怪啊