部分程序如下:
DateTime ntime = DateTime.Today.AddHours( DateTime.Now.Hour );
string time=ntime.ToString();
我用MessageBox.Show(time)查看time的值,为2007-08-15 13:00:00,没有错。
但是下面这条语句
string commandText="select count(*) from table where Convert(Varchar, 日期时间, 120) = 'time' and 区站号 = '56038'";
程序结果就不对了,是不是由于Varchar和string不能比较啊?但是我在time上加了单引号啊。
如果我把'time'改为'2007-08-15 13:00:00',那么程序结果就对了。
各位前辈不吝赐教啊,谢谢^_^

解决方案 »

  1.   

    string commandText="select count(*) from table where Convert(Varchar, 日期时间, 120) = '"+time+"' and 区站号 = '56038'";写法有问题=.=
      

  2.   

    你这个写法是把 Convert(Varchar, 日期时间, 120) 和  这个字符串 “time”进行比较
    而不是和time变量的值进行比较,应按照楼上的写法来写
      

  3.   

    外边的变量应该用+time+连起来
    否则
    你比较的只是
    time=2007-08-15 13:00:00
    当然不等
      

  4.   

    谢谢feiyingy3,eaglet,feiyingy3三位前辈指点,我试试^_^
      

  5.   

    string commandText="select count(*) from table where 区站号 = '56038' and
    Convert(Varchar, 日期时间, 120) = "+time;  这样也行
      

  6.   

    还是不行啊,我把程序改成
    string commandText="select count(*) from table where Convert(Varchar, 日期时间, 120) = '"+time+"' and 区站号 = '56038'";

    string commandText="select count(*) from table where Convert(Varchar, 日期时间, 120) = '+time+' and 区站号 = '56038'";
    程序结果都是错的,但是如果我把'time'改为'2007-08-15 13:00:00',那么程序结果就对了。
    各位前辈指点啊^_^谢谢了。
      

  7.   

    改成
    string commandText="select count(*) from table where Convert(Varchar, 日期时间, 120) = "+time+" and 区站号 = '56038'";
    也不行:(
      

  8.   

    Convert(Varchar, 日期时间, 120) = '"+time+"'这个,数据库里的字段名呢?
      

  9.   

    where time这样不太好吧?还是where id比较好