2012-04-23 153 views
0

在SQL Server数据库中,时间戳以毫秒添加。但是在检索时,毫秒不会开始返回,即如果时间戳为2012-04-23 15:03:35.345 PM,那么在检索时,我只能得到2012-04-23 15:03:35.000 PM。我用下面的代码检索数据:如何在从SQL Server检索时以毫秒为单位获取时间戳?

query = "Select * from database"; 
_cmd = new SqlCommand(query, _con); 

SqlDataAdapter da = new SqlDataAdapter(_cmd); 
DataTable dt = new DataTable(); 
da.Fill(dt); 

foreach (DataRow dr in dt.Rows) 
{ 
    string str = Convert.ToDateTime(dr[0].ToString()).ToString("yyyy-MM-ddhh:mm:ss.fff"); 
    dt[0] = str; 
} 

我怎样才能得到毫秒?

回答

2

dr[0]可能已经是DateTime并将其转换为字符串并返回到DateTime将不会有任何好处。我想你需要将其强制转换:

string str = ((DateTime)dr[0]).ToString("yyyy-MM-ddhh:mm:ss.fff"); 

使用调试器通过选择整个表达式(或只是有趣的一部分,你的情况dr[0])来检查表达式的类型,右键单击选择,然后选择'快速监视”。列表中的最后一列是类型。

相关问题