sqlstr="select *f rom a where c='12'"
如果这条语句查询不出结果.则查询
sqlstr="select *f rom a where like'12'"
这两个查询语句能不能合在一起呢?
是不是要两次调用数据库来查询.我的意思就是是不是要打开关闭两次数据库呢?

解决方案 »

  1.   

    你可以写成
    sqlstr="select * from a where c like '%12%'" 
    这样得到的结果集包含 
    sqlstr="select *f rom a where c='12'" 
    的结果集,写这一句就可以了
      

  2.   

    sqlstr="select *f rom a where like'12'" 
    没有任何占位符合等于没区别吧
      

  3.   


    sqlstr="select * from a where c like '%12'";//前匹配,匹配末尾为12的所有数据
    sqlstr="select * from a where c like '12%'";//后匹配,匹配前端为12的所有数据
    sqlstr="select * from a where c like '%12'";//全匹配,匹配含有12的所有数据sqlstr="select * from a where MID(c,1,2)='12'";//固定匹配,取字段前2个字符进行匹配,看是否为12
      

  4.   

    sqlstr="select * from a where c like '%12%'";//全匹配,匹配含有12的所有数据
      

  5.   

    这样的话就必须要写两个 数据库操作方法了
     你要先查出C="12"
    然后才能用like 查询
      

  6.   

    你是不想要先进行精确查询,然后再进行模糊查询呀?
    只要你两条语句之前没有对数据库执行conn.close().就没有打开关闭两次数据库,只是对已经打开的数据库进行两次查询。个人理解
      

  7.   

    他的like和=后的条件完全一模一样。like后没有用到?或%,还有就是like那句错误,没指明字段!
      

  8.   

    那你在代码中控制sqlstr="select * from a where c='12'" ;
    sqlstrlike="select * from a where c like  '%12%'"
    SqlConnection connection = new SqlConnection(conn); 
    SqlCommand reader = new SqlCommand(sql, connection); 
    SqlDataReader sdr = reader.ExecuteReader(); 
    if (sdr.HasRows) 
    {
     ....
    }
    else
    {
    reader.commandtext=sqlstrlike
    sdr=reader.ExecuteReader(); 
    }供你参考.
      

  9.   

    like应用到?或%,不用再打开关闭两次数据库,你可以先施行模糊查询把结果保存在table中,如模糊查询没查到则返回为查到结果,如模糊查询有结果再对table进行精确查询。
      

  10.   

    看来你对Sql中的like查询不了解啊!
    select * from a where c='%12%' //这句的意思是查询c中所有包含12的
    select * from a where c='12%' /这句是所有前端包含12的楼主若想深入
    请到我的资源里面去下载: http://download.csdn.net/source/448361(只要2分)
    呵呵!
      

  11.   

    select * from a where c like '%12%'