2011-10-08 28 views

回答

1

即時消息,你可以使用ParseExact方法:

DateTime date = DateTime.ParseExact("1/1/1300", "d/M/yyyy", CultureInfo.InvariantCulture); 

或使用TryParseExact如果日期可能是在錯誤的格式或表示無效日期:

DateTime date; 
if (DateTime.TryParseExact("1/1/1300", "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) 
{ 
    // parsing was successful => you could use the date here 
} 
+0

我試着在linq查詢Datetime.ParseExact,它給了我這個錯誤。方法'System.DateTime ParseExact(System.String,System.String,System.IFormatProvider)'沒有支持轉換爲SQL –

0

有在C#中沒有DateTime2,只是DateTime:

string dateString = "1/1/1300"; 
DateTime date = DateTime.Parse(dateString, CultureInfo.InvariantCulture); 
0

如果你在找什麼fo r被轉換datetime2 SQL數據類型的.NET數據類型,檢查這個答案:

Conversion of a datetime2 data type to a datetime data type results out-of-range value

這似乎表明,類型是相同DateTime不過,在回答的片段:

new DataColumn("myDate", typeof(DateTime)) 

我假設你使用的是DataReader,你也可以把這個值作爲DateTime,或者。

如果您使用的是ORM,它將取決於所使用的ORM。如果這仍然沒有幫助,您可能需要指定如何訪問數據庫。

相關問題