2012-04-23 155 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])來檢查表達式的類型,右鍵單擊選擇,然後選擇'快速監視」。列表中的最後一列是類型。

相關問題