日期:2014-05-18  浏览次数:20503 次

求救?页面上有三个文本框textbox1,textbox2,textbox3和一个查询按钮,任意一个或组合输入条件后,请问查询代码或算法?
任意一个或组合输入条件后,请问查询代码或算法?

------解决方案--------------------
最简单的方法:VS2005
protected void btnQuery_Click(object sender,EventArgs e) //查询按钮事件
{
string sql= "select * from your_table WHERE 1=1 "; //sql查询语句
if(!string.IsNullOrEmpty(TextBox1.Text.Trim()))
{
sql += " AND condtion1= ' "+TextBox1.Text.Trim()+ " ' ";
//注意int字段和varchar字段的区别,以下同理
}
if(!string.IsNullOrEmpty(TextBox2.Text.Trim()))
{
sql += " AND condtion2= ' "+TextBox2.Text.Trim()+ " ' ";
}
if(!string.IsNullOrEmpty(TextBox3.Text.Trim()))
{
sql += " AND condtion3= ' "+TextBox3.Text.Trim()+ " ' ";
}
//根据sql去取你需要的数据
}
------解决方案--------------------
给你一个思路,仅你参考

sql = "select * from abc where id > 0 "

if (textbox1.text != " ")
sql += " AND a = " + textbox1.text

if (textbox2.text != " ")
sql += " AND b = " + textbox2.text

if (textbox3.text != " ")
sql += " AND c = " + textbox3.text

response.write(sql)
------解决方案--------------------
//数据库语句封装查询出记录,默认查询结果为全部
StringBuilder strCommand = new StringBuilder();
strCommand.Append( " SELECT * from Table_Name WHERE (1=1) AND ");
//当TextBox1不为空,(精确查询)
if (this.TextBox1.Text.ToString().Trim().Length != 0)
{
strCommand.Append( " AND Table_Name.字段1= ' ");
strCommand.Append(this.TextBox1.Text.ToString().Trim()).Append( " ' ");
}
//当TextBox2不为空,(精确查询)
if (this.TextBox2.Text.ToString().Trim().Length != 0)
{
strCommand.Append( " AND Table_Name.字段2= ' ");
strCommand.Append(this.TextBox2.Text.ToString().Trim()).Append( " ' ");
}
//当TextBox3不为空,(模糊查询)
if (this.TextBox1.Text.ToString().Trim().Length != 0)
{
strCommand.Append( " AND Table_Name.字段3 like '% ");
strCommand.Append(this.TextBox3.Text.ToString().Trim()).Append( "% ' ");
}
//根据某一字段的升序排列
strCommand.Append( " ORDER BY Table_Name.字段* ASC ");