现在有张学生表s,表的字段有,学号=A,姓名=B,性别=C,民族=D。如图,我可以根据以上1-4条条件来查询s表的数据。
第一,我可以根据无条件来查询此表的数据。
第二,我可以根据1个条件来查询此表的数据。
第三,2个以此类推到4个条件查询s表数据。
我个人做法是:用if(){}..else if(){}...else{}来判断查询条件进而查询到数据,按照我的方法的话要写的判断语就要如下这么多:
1.单个查询条件都通过。
A,B,C,D
2.两个查询条件
AB,AC,AD,
BC,BD,
CD,
3.三个查询条件
ABC,ABD
ACD
BCD
4.四个查询条件
ABCD
假如我的查询条件是4个以上我就要写的代码量就更多了,我想知道有没有简便的方法,我这样写的方法有点笨????求教了
第一,我可以根据无条件来查询此表的数据。
第二,我可以根据1个条件来查询此表的数据。
第三,2个以此类推到4个条件查询s表数据。
我个人做法是:用if(){}..else if(){}...else{}来判断查询条件进而查询到数据,按照我的方法的话要写的判断语就要如下这么多:
1.单个查询条件都通过。
A,B,C,D
2.两个查询条件
AB,AC,AD,
BC,BD,
CD,
3.三个查询条件
ABC,ABD
ACD
BCD
4.四个查询条件
ABCD
假如我的查询条件是4个以上我就要写的代码量就更多了,我想知道有没有简便的方法,我这样写的方法有点笨????求教了
string sql="select * from tb1 where 1=1 ";
if(!string.IsNullOrEmpty(textbox1.Text.Trim()))
{
sql+=" and username=@name";
}
//依次类推
不是一样吗??
一个的查询条件方法如下:
if(textbox1.Text.Trim()!=""&&textbox2.Text.Trim()==""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}
else if(textbox1.Text.Trim()==""&&textbox2.Text.Trim()!=""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}以此类推
两个条件时用:
else if(textbox1.Text.Trim()!=""&&textbox2.Text.Trim()!=""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}以此类推...
三个条件时也是跟上面差不多的方法,我觉得有点麻烦不知有没有简便的方法???????
当它的查询条件有10个或者更多的时候,我写的判断语句会更复杂代码量会更多,虽然它的判断条件可以用数据的方法来组合出来但是这样真的太复杂求教简便的????????
跟我的if(){}..........else if(){}...........else
不是一样吗??
一个的查询条件方法如下:
if(textbox1.Text.Trim()!=""&&textbox2.Text.Trim()==""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}
else if(textbox1.Text.Trim()==""&&textbox2.Text.Trim()!=""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}以此类推
两个条件时用:
else if(textbox1.Text.Trim()!=""&&textbox2.Text.Trim()!=""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}以此类推...
三个条件时也是跟上面差不多的方法,我觉得有点麻烦不知有没有简便的方法???????
当它的查询条件有10个或者更多的时候,我写的判断语句会更复杂代码量会更多,虽然它的判断条件可以用数据的方法来组合出来但是这样真的太复杂求教简便的????????你还是没明白楼上的意思。
sql+=" and username=@name"; 就是拼写sql语句。string sql="select * from tb1 where 1=1 ";
if(!string.IsNullOrEmpty(textbox1.Text.Trim()))
{
sql+=" and b=@b";
}
else if(!string.IsNullOrEmpty(textbox2.Text.Trim()))
{
sql+=" and a=@a";
}
else if(!string.IsNullOrEmpty(textbox3.Text.Trim()))
{
sql+=" and c=@c";
}
else if(!string.IsNullOrEmpty(textbox4.Text.Trim()))
{
sql+=" and d=@d";
}
if(!string.IsNullOrEmpty(textbox1.Text.Trim()))
{
sql+=" and username=@name";
}
if(!string.IsNullOrEmpty(textbox2.Text.Trim()))
{
sql+=" and 民族=@minzu";
}
if(!string.IsNullOrEmpty(textbox3.Text.Trim()))
{
sql+=" and Num=@num";
}
if(!string.IsNullOrEmpty(textbox4.Text.Trim()))
{
sql+=" and gander=@gander";
}
//这样就够了 不管几个条件 都可以!!
http://www.soaspx.com/dotnet/vb.net/vb.net_20091017_1026.html
不是一样吗??
一个的查询条件方法如下:
if(textbox1.Text.Trim()!=""&&textbox2.Text.Trim()==""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}
else if(textbox1.Text.Trim()==""&&textbox2.Text.Trim()!=""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}以此类推
两个条件时用:
else if(textbox1.Text.Trim()!=""&&textbox2.Text.Trim()!=""&&textbox3.Text.Trim()==""&&textbox4.Text.Trim()==""){}以此类推...
三个条件时也是跟上面差不多的方法,我觉得有点麻烦不知有没有简便的方法???????
当它的查询条件有10个或者更多的时候,我写的判断语句会更复杂代码量会更多,虽然它的判断条件可以用数据的方法来组合出来但是这样真的太复杂求教简便的????????你还是没明白楼上的意思。
sql+=" and username=@name"; 就是拼写sql语句。string sql="select * from tb1 where 1=1 ";
if(!string.IsNullOrEmpty(textbox1.Text.Trim()))
{
sql+=" and b=@b";
}
else if(!string.IsNullOrEmpty(textbox2.Text.Trim()))
{
sql+=" and a=@a";
}
else if(!string.IsNullOrEmpty(textbox3.Text.Trim()))
{
sql+=" and c=@c";
}
else if(!string.IsNullOrEmpty(textbox4.Text.Trim()))
{
sql+=" and d=@d";
}
我明了,谢谢各位的回答
public static string GetSQLString(string SQL, string Order, List<string> ArrayWhere)
{
bool boolWhere = false;
StringBuilder sbWhere = new StringBuilder();
if (ArrayWhere.Count > 0)
{
foreach (string commandtext in ArrayWhere)
{
if (commandtext != "")
{
if (!boolWhere)
{
sbWhere.AppendFormat(" where {0}", commandtext);
boolWhere = true;
}//如果为第一个where语句则加入“where”关键字
else
{
sbWhere.AppendFormat(" and {0}", commandtext);
}//否则加入“and”关键字
}//如果条件语句不为空则进行SQL语句组合
}
}
sbWhere.AppendFormat(" {0}", Order);
SQL = SQL + sbWhere.ToString();
return SQL;
}
if(!string.IsNullOrEmpty(textbox1.Text.Trim()))
{
sql+=" and username=@name";
}已经很节省工作了比如你又三个,可以直接
string sql="select * from tb1 where 1=1 ";
if(!string.IsNullOrEmpty(textbox1.Text.Trim()))
{
sql+=" and username=@name";
}
if(!string.IsNullOrEmpty(textbox2.Text.Trim()))
{
sql+=" and username=@address";
}
if(!string.IsNullOrEmpty(textbox3.Text.Trim()))
{
sql+=" and username=@age";
}等等,节省了大量工作,楼主还需要实现怎样的效果?当然,如果你的textbox实在很多的话,可以在textbox的tag中写上该textbox对应数据库字段的name,如下
foreach(control c in controls)
{
if(c.gettype()==typeof(textbox)&&c.tag!=null)
{
if(!string.IsNullOrEmpty(((textbox)c).Text.Trim()))
sql+=" and username=@"+c.tag.tostring().trim();
}
}
没区分大小写,楼主可以借鉴