2017-04-06 100 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"); 
} 
相关问题