数据库是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;为何就得不到数据呢?
在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;为何就得不到数据呢?
select * from Table1 where date1 = convert(datetime,'2009-02-27',120)";
select * from Table1 where date1 = convert(char(10),convert(datetime,'2009-02-27'),120)
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();有数据,不方便贴图。
调试一下,try.....catch一下看看有没有异常
语句没有问题
2、用Sql Server Profile截获通过程序运行执行的SQL语句,看语句有没有问题
:-)
谢谢各位参与, 详细可以参考一下 http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/3/5.aspx
最后鄙视一下2楼.
如果在程序中执行select * from Table1都没有查到数据,那就不是日期的问题了。
如果说能返回数据,但是程序获取的返回行数却是0,那绝对是程序问题。