DataTable1TableAdapter中的GetData()查询:select * from Table1
下面是一个条件查询的方法
private string GetWhere()
{
string wherestr = "where";
if (textBox1.Text.Trim() != "")
{
wherestr += "test.EntryId like '%" + textBox1.Text.Trim() + "%'";
}
if (textBox2.Text.Trim() != "")
{
wherestr += "test.PrintPerson like '%"+textBox2.Text.Trim()+"%'";
}
if (textBox3.Text.Trim() != "")
{
wherestr += "type.TypeName like '%"+textBox3.Text.Trim()+"%'";
}
if (this.dateTimePicker1.Text.Trim() != "")
{
DateTime dt = new DateTime();
if (DateTime.TryParse(this.dateTimePicker1.Text.Trim(), out dt) == true)
{
wherestr += "and test.PrintDate >='" + dt.ToString("yyyy-MM-dd") + "00:00:00" + "'";
wherestr += "and test.PrintDate<='"+dt.ToString("yyyy-MM-dd")+"23:59:59"+"'";
}
}
wherestr += "order by test.SeqId asc";
return wherestr; }
怎样把这个GetWhere方法返回的查询条件添加到GetData()里也加时连到select * from Table1后面?
下面是一个条件查询的方法
private string GetWhere()
{
string wherestr = "where";
if (textBox1.Text.Trim() != "")
{
wherestr += "test.EntryId like '%" + textBox1.Text.Trim() + "%'";
}
if (textBox2.Text.Trim() != "")
{
wherestr += "test.PrintPerson like '%"+textBox2.Text.Trim()+"%'";
}
if (textBox3.Text.Trim() != "")
{
wherestr += "type.TypeName like '%"+textBox3.Text.Trim()+"%'";
}
if (this.dateTimePicker1.Text.Trim() != "")
{
DateTime dt = new DateTime();
if (DateTime.TryParse(this.dateTimePicker1.Text.Trim(), out dt) == true)
{
wherestr += "and test.PrintDate >='" + dt.ToString("yyyy-MM-dd") + "00:00:00" + "'";
wherestr += "and test.PrintDate<='"+dt.ToString("yyyy-MM-dd")+"23:59:59"+"'";
}
}
wherestr += "order by test.SeqId asc";
return wherestr; }
怎样把这个GetWhere方法返回的查询条件添加到GetData()里也加时连到select * from Table1后面?
string sql = "select * from Table1" + GetWhere();
这样不行吗?
{
string sql=string.Format("select * from table where 1=1 {0}",strwhere)
} 每个查询条件要加and 例如 "and test.EntryId like '%" + textBox1.Text.Trim() + "%'";执行 GetData( GetWhere() );
那就把GetWhere()返回结果添加上不可以么