Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SSIS脚本转换格式化日期和日期时间_C#_Ssis - Fatal编程技术网

C# 使用SSIS脚本转换格式化日期和日期时间

C# 使用SSIS脚本转换格式化日期和日期时间,c#,ssis,C#,Ssis,我有一个平面文件,其中包含如下日期:07/07/2003 12:18:20 PM SSIS转换输出列设置为数据库时间戳[DT_DBTIMESTAMP] 我有以下方法: public string DbDateTime(string input) { return DateTime.ParseExact(input, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture).ToString(

我有一个平面文件,其中包含如下日期:
07/07/2003 12:18:20 PM

SSIS转换
输出列
设置为数据库
时间戳[DT_DBTIMESTAMP]

我有以下方法:

    public string DbDateTime(string input)
    {
        return DateTime.ParseExact(input, "M/d/yyyy h:mm:ss tt", 
            CultureInfo.InvariantCulture).ToString();
    }
我需要这样的输出到数据库:

2003-07-07 12:18:00.000
但是,我不断得到一个错误:

字符串未被识别为有效的日期时间

输出设置如下:

Row.OuputDateTimeColumn = 
Convert.ToDateTime(DbDateTime(Row.InputDateTimeColumn));

我不喜欢使用
派生列进行转换。

您的格式错误。它与您的输入字符串不匹配。改用
MM/dd/yyyy h:MM:ss tt
格式。比如,

public string DbDateTime(string input)
{
    return DateTime.ParseExact(input,
                               "MM/dd/yyyy h:mm:ss tt", 
                               CultureInfo.InvariantCulture).
                               ToString("yyyy-MM-dd h:mm:ss.fff");
}

将日期和时间的指定字符串表示形式转换为其 使用指定格式和特定区域性的DateTime等效项 格式信息字符串表示形式的格式必须匹配 指定的格式完全相同。

用于
1
12
,但用于
01
12

用于
1
31
,但用于
01
31

也要小心你的小时表
h
表示
1
12
hh
表示
01
12
。如果要使用24小时格式,则需要使用
H
HH
格式

欲了解更多信息,请查看


问题1:您为月份和日期字段提供的自定义格式无效

您的日期字符串中有两个固定数字,但您只提供一个数字格式,如下所示:

"M/d/yyyy h:mm:ss tt"
Row.OuputDateTimeColumn = DateTime.ParseExact(
   DbDateTime(Row.InputDateTimeColumn),"yyyy-MM-dd h:mm:ss.fff"  
       CultureInfo.InvariantCulture);
因此,您需要按如下方式替换此项:

"M/d/yyyy h:mm:ss tt"
Row.OuputDateTimeColumn = DateTime.ParseExact(
   DbDateTime(Row.InputDateTimeColumn),"yyyy-MM-dd h:mm:ss.fff"  
       CultureInfo.InvariantCulture);
如果日期字符串小时格式为
00-12

"MM/dd/yyyy hh:mm:ss tt"
 "MM/dd/yyyy h:mm:ss tt"
如果日期字符串小时格式为
0-12

"MM/dd/yyyy hh:mm:ss tt"
 "MM/dd/yyyy h:mm:ss tt"
问题2:您希望以自定义格式返回日期字符串,格式为
2003-07-07 12:18:00.000
我假定它的格式为
yyyy-MM-dd h:MM:ss.ffff

注意:在您的示例中,
日期
是相同的(07),如果我的假设是错误的,您需要相应地调整它们

因此,您需要为return语句中的
ToString()
函数提供上述自定义格式

完整代码:

public string DbDateTime(string input)
{
 return DateTime.ParseExact(input, "MM/dd/yyyy h:mm:ss tt", 
        CultureInfo.InvariantCulture).ToString("yyyy-MM-dd h:mm:ss.fff");
}
问题3:在向数据库添加自定义日期格式时,需要首先将其转换为日期时间,如下所示:

"M/d/yyyy h:mm:ss tt"
Row.OuputDateTimeColumn = DateTime.ParseExact(
   DbDateTime(Row.InputDateTimeColumn),"yyyy-MM-dd h:mm:ss.fff"  
       CultureInfo.InvariantCulture);

您好,我的朋友们:仍然收到相同的错误:字符串未被识别为有效的日期时间。@UnserageGuy:您提供的是哪种格式?日期字符串的格式是什么?A小时有两个固定数字吗?你能发布你的最新代码吗?我遵循这里发布的确切代码:我创建了一个数据类型为:
数据库时间戳[DT_DBTIMESTAMP]
的输出列。当我在一个控制台应用程序中测试这样的代码时,我注意到了这一点。我从一开始就得到了相同的日期格式:
Console.Write(DateTime.ParseExact(DbDateTime(“07/07/2003 12:18:20 PM”),“yyyy-MM-dd h:MM:ss.fff”,CultureInfo.InvariantCulture))@unserageguy:好的,让我再检查一下。凯:这个有效!我将方法更改为键入
DateTime
`public DateTime DbDateTime(字符串输入){return DateTime.ParseExact(输入,“M/d/yyyy h:mm:ss tt”,CultureInfo.InvariantCulture);}`然后将方法的输出转换为输出列
Row.OuputDateTimeColumn=Convert.ToDateTime(Row.InputDateTimeColumn)