数据库是sql server, 一个Table1, 里面有个smalldatetime字段date1, 
在Microsoft SQL Server Management Studio 下写sql语句select * from Table1 where date1 = '2009-02-27', 执行正常.
在C#下
Connection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["DataBaseConnectionString"].Trim();
if (Connection.State == ConnectionState.Closed)
   Connection.Open();SqlCommand command = new SqlCommand();
command.CommandText = "select * from Table1 where date1 = '2009-02-27'";
command.Connection = Connection;SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;为何就得不到数据呢?

解决方案 »

  1.   

    select * from Table1 where datediff(dd,date1,'2009-02-27')=0
      

  2.   

    //试试
    select * from Table1 where date1 = convert(datetime,'2009-02-27',120)"; 
      

  3.   

    //试试
    select * from Table1 where date1 = convert(char(10),convert(datetime,'2009-02-27'),120)
      

  4.   

    你去试试select * from Table1 where date1 = '2009-02-27'在Microsoft SQL Server Management Studio 下能不能检索出数据
      

  5.   

    3,6,7楼的写法在Microsoft SQL Server Management Studio能检索出来, 但是在C#下, 就不行哦, 所以说感觉很奇怪, 哪位能在C#下试一下
      

  6.   

    create table Test(
    SID int,
    SName varchar(20),
    SBorn smalldatetime
    );insert into Test values(1,'A','2009-3-1');
    insert into Test values(2,'B','2009-3-2');
    insert into Test values(3,'B','2009-3-3');
    select * from Test where SBorn='2009-3-2';后台:
    SqlConnection Connection = new SqlConnection();
                    Connection.ConnectionString = ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;
                    if (Connection.State == ConnectionState.Closed)
                        Connection.Open();                SqlCommand command = new SqlCommand();
                    command.CommandText = "select * from Test where SBorn='2009-3-2'";
                    command.Connection = Connection;                SqlDataAdapter da = new SqlDataAdapter(command);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    GridView1.DataSource = ds;
                    GridView1.DataBind();有数据,不方便贴图。
      

  7.   

    这样是可以查询出数据来的
    调试一下,try.....catch一下看看有没有异常
    语句没有问题
      

  8.   

    看代码应该是没有问题,应该检查一下你的gridview设置
      

  9.   

    代码是没有错, 如果不加条件这样的话select * from Table1是可以检索出数据, 系统不报错,只是返回的数据行数为0, 问题应该是在条件上 where date1 = '2009-02-27', 我用的数据库是sql server 2005英文版, 前端操作系统是xp 英文版, c#也是英文版, 会不会是英文版的原因导致日期格式不同, 所以检索不出数据
      

  10.   

    问题就在这里, 在sql server下加了条件查询有数据, 但在c#下就检索不出数据 
      

  11.   

    1、检查连接字符串看是否连接的同一个数据库
    2、用Sql Server Profile截获通过程序运行执行的SQL语句,看语句有没有问题
    :-)
      

  12.   

    问题解决了, 果然是sql server英文版的问题, 条件改为where FileDate='Mar 5 2008' 就ok了.
    谢谢各位参与, 详细可以参考一下 http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/3/5.aspx
    最后鄙视一下2楼.
      

  13.   


    如果在程序中执行select * from Table1都没有查到数据,那就不是日期的问题了。
    如果说能返回数据,但是程序获取的返回行数却是0,那绝对是程序问题。