2017-04-06 57 views
0

我試圖從psql數據庫中提取數據並使用BizTalk將它們插入到SQL Server數據庫中。有一個叫TimeStampcreateddate與時區一樣6/30/2016 12:00:00 AM將字符串轉換爲SQL Server中的DateTimeoffset

我想這些數據插入到被稱爲datetimeoffsetDateCreated SQL Server列在psql裏的一列。由於我使用BizTalk所有的數據都爲刺痛處理,所以我用下面的腳本像

public string ConvertDateCreated(string dateCreated) 
{ 
    System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture; 
    return DateTime.ParseExact(dateCreated, "MMddyyyy", provider).ToString("yyyyMMdd"); 
} 

enter image description here

但它拋出一個錯誤:

String was not recognized as a valid DateTime.

Exception type: FormatException
Source: mscorlib
Target Site: System.DateTime ParseExact(System.String, System.String, System.Globalization.DateTimeFormatInfo, System.Globalization.DateTimeStyles)

The following is a stack trace that identifies the location where the exception occurred

at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at System.Xml.Xsl.CompiledQuery.Script1.ConvertDateCreated(String dateCreated)
at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver)

+0

你有沒有從原始數據庫返回的日期字符串的例子? – PaulF

+0

這就像'2016/6/30 12:00:00 AM' – user4912134

+0

然後你的格式應該是'dd/MM/yyyy HH:mm:ss' – Nived

回答

0

對於ParseExact,您源日期格式應該與"M/dd/yyyy hh:mm:ss tt"一樣。

public static string ConvertDateCreated(string dateCreated) 
{ 
    System.Globalization.CultureInfo provider = 
     System.Globalization.CultureInfo.InvariantCulture; 
    return DateTime.ParseExact(dateCreated, "M/dd/yyyy hh:mm:ss tt", provider) 
      .ToString("yyyyMMdd"); 
} 
相關問題