0

我正在使用 SSIS 将数据从 CSV 文件上传到我的数据库中的表中。我尝试获取 2 个字符串(一个用于日期,另一个用于时间)并像这样连接它们:

string datsDateTime = String.Concat(Row.DATE, " ", Row.HEURE);     

然后,我尝试像这样解析它:

Row.DateTime = DateTime.ParseExact(datsDateTime, "dd/MM/yyyy HH:mm", null);

但它不起作用。当我尝试测试这个脚本时,我得到了这个错误:

String was not recognized as a valid DateTime.
   at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
   at ScriptMain.Input0_ProcessInputRow(Input0Buffer Row)
   at UserComponent.Input0_ProcessInput(Input0Buffer Buffer)
   at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.ProcessInput(Int32 inputID, PipelineBuffer buffer)

我正在使用 Visual Studio 2008。有什么想法吗?

编辑:在我的 csv 文件中的“日期”列中,日期是一个字符串,如下所示:20120101(2012 年,月份为 01,当天为 01)。

更新:好的,我在粘贴任何内容之前通过这样做找到了解决方案
string datsDateTime = Row.DATE.Insert(6,"/").Insert(4, "/") + " " + Row.HEURE; 。希望它会有所帮助。

4

1 回答 1

1

您可能应该将格式更改为

Row.FinalDate = DateTime.ParseExact(datsDateTime, "yyyyMMdd HH:mm", null);

由于您以yyyymmdd格式获取日期日期不是存储为string 而是存储为integers。如果您想以 dd/mm/yyyy 格式显示数据,那么您可以使用具有特定格式的 ToString() 方法,例如

YourDate.ToString("dd/MM/yyyy")

但是由于您将存储在数据库中,因此它将存储为整数检查您的 sql server 中的 Dateformat

dbcc useroptions
于 2013-05-03T09:52:32.170 回答