日期:2014-05-19  浏览次数:20829 次

求助,.net问题
int   i=0;
if(str_name!= " "   ||   str_nrjj!= " "   ||   str_memo!= " "   ||   str_state!= " "   ||   str_jystate!= " ")
          {
str_tj= "where ";
                  if(str_name!= " ")
                  {
i=1;
str_tj=str_tj+ "name   like   '% "+str_name+ "% ' ";
}
if(str_press!= " ")
{
                  if(i==1)
str_tj=str_tj+ "and   press   like   '% "+str_press+ "% ' ";
else
{
str_tj=str_tj+ "press   like   '% "+str_press+ "% ' ";
i=1;
}
}
if(str_nrjj!= " ")
{
if(i==1)
str_tj=str_tj+ "and   nrjj   like   '% "+str_nrjj+ "% ' ";
else
{ str_tj=str_tj+ "nrjj   like   '% "+str_nrjj+ "% ' ";
i=1;
}
}
if(str_memo!= " ")
{
if(i==1)
str_tj=str_tj+ "and   memo   like   '% "+str_memo+ "% ' ";
else
{ str_tj=str_tj+ "memo   like   '% "+str_memo+ "% ' ";
i=1;
}
}
if(str_state!= " ")
{
if(i==1)
str_tj=str_tj+ "and   state=   '% "+str_state+ "% ' ";
else
{
str_tj=str_tj+   "state=   '% "+str_state+ "% ' ";
i=1;
}
}
if(str_jystate!= " ")
{
if(i==1)
str_tj=str_tj   +   "and   jystate=   '% "+str_jystate+ "% ' ";
else
{
str_tj=str_tj   +   "jystate=   '% "+str_jystate+ "% ' ";
i=1;
}
}
}
else
str_tj= " ";
string   cnnstr= "Persist   Security                         Info=false;userid=sa;password=;database=uds; ";
SqlConnection   cnn;
cnn   =   new   SqlConnection(cnnstr);
cnn.Open();
SqlCommand   cmd   =   new   SqlCommand( "Select   count(*)   as   nums   from   UDS_BOOK_INFO "   +str_tj,cnn);


好像是查询字符串出错,但是基础知识太差,不知怎么改正。请求帮助,谢谢了先~

------解决方案--------------------
str_tj+ "name like '% "+str_name+ "% ' ";

应该加个空格把

str_tj+ " name like '% "+str_name+ "% ' ";


其他的地方类似
------解决方案--------------------
每个双引号都留1个空格出来
否则就胡子连着眉毛了
------解决方案--------------------
加空格,还有就是用Stringbuilder 不要用这种方式了,效率太差.
------解决方案--------------------
同一楼,if...else里拼接语句前加空格,还有
SqlCommand cmd = new SqlCommand( "Select count(*) as nums from UDS_BOOK_INFO " +str_tj,cnn);
这里也要加
SqlCommand cmd = new SqlCommand( "Select count(*) as nums from UDS_BOOK_INFO " +str_tj,cnn);

另外感觉楼主这样写else语句拼接之后会有问题,给其中几个变量赋值,另外几个留空,设断点看下最后的str_tj是什么样的,然后从这上面入手,修改代码


------解决方案----------------