1

我有一个将数据读取器转换为 CSV 的函数。SQL 返回YYYY-MM-DD HH:MM:SS.000而 .net 将其转换为MM/DD/YYYY值。我想检查数据读取器中的值是否为 DateTime 并将其转换回YYYY-MM-DD HH:MM:SS.000格式。

更新:

代码:

Public Function DR_To_CSV_Stream() As Boolean
    Dim FirstLine As String = ""
    Dim retVal As Boolean

    m_DebugMessage = m_DebugMessage & "DR_To_CSV_Stream: "

    If m_Datareader.Read Then 'CREATE HEADER NAMES
        m_DebugMessage = m_DebugMessage & "CREATE HEADER NAMES: "
        For intcount = 0 To m_Datareader.FieldCount - 1
            If intcount <> m_Datareader.FieldCount - 1 Then
                sw.Write(String.Format("""{0}"",", m_Datareader.GetName(intcount)))
                FirstLine = FirstLine + String.Format("""{0}"",", m_Datareader.Item(intcount))
            Else
                sw.Write(String.Format("""{0}""", m_Datareader.GetName(intcount)))
                FirstLine = FirstLine + String.Format("""{0}""", m_Datareader.Item(intcount))
            End If
        Next
        sw.WriteLine("")
        m_DebugMessage = String.Format("{0}WriteLine(FirstLine): {1}", m_DebugMessage, FirstLine)
        sw.WriteLine(FirstLine)

        Do While m_Datareader.Read() 'READ DATA AND CREATE ROWS
            m_DebugMessage = m_DebugMessage & "READ DATA AND CREATE ROWS: "
            For intcount = 0 To m_Datareader.FieldCount - 1
                If intcount <> m_Datareader.FieldCount - 1 Then
                    sw.Write(String.Format("""{0}"",", m_Datareader.Item(intcount)))
                Else
                    sw.Write(String.Format("""{0}""", m_Datareader.Item(intcount)))
                End If
            Next
            sw.WriteLine("")
        Loop
        m_Datareader.Close()
        m_DebugMessage = m_DebugMessage & "m_Datareader.Close(): "
        m_Datareader = Nothing
    Else
        m_DebugMessage = m_DebugMessage & "m_Datareader.Read = FALSE: "
    End If
    sw.Flush() 'COMMIT LAST SECTION TO WRITER
    If sw.BaseStream Is Nothing Then
        m_DebugMessage = m_DebugMessage & "  |  |NOTHING IN THE STREAM: "
    Else
        m_DebugMessage = m_DebugMessage & "  |  |THERE IS SOMETHING IN THE STREAM: "
    End If


    m_sr = New IO.StreamReader(sw.BaseStream)
    m_sr.BaseStream.Seek(0, System.IO.SeekOrigin.Begin)
    m_ReturnStream = m_sr.BaseStream

    If m_ReturnStream.CanRead Then
        retVal = True
        m_DebugMessage = m_DebugMessage & "  |  | 2ND TIME - THERE IS SOMETHING IN THE STREAM: "

    Else
        retVal = False
        m_DebugMessage = m_DebugMessage & "  |  |NOTHING IN THE RETURN STREAM: "
    End If



    Return retVal

End Function

此函数将 datareader 转换为 CSV 流。在另一个函数中,我将此流转换为字符串并将其写入文件:

File.WriteAllBytes(outboundFileFullPath, Encoding.ASCII.GetBytes(data))

希望这有助于理解我的问题。

或者另一种方式是:将 MemoryStream 直接写入文件。我试过了,但没有用。谢谢!

更新#2 这是我根据 Romil 的建议所做的:

   Dim dateValue As Date
                    If intcount <> m_Datareader.FieldCount - 1 Then
                        If (DateTime.TryParse(m_Datareader.Item(intcount).ToString(), dateValue)) Then
                            sw.Write(String.Format("""{0}"",", dateValue.ToString("yyyy-MM-dd")))
                        Else
                            sw.Write(String.Format("""{0}"",", m_Datareader.Item(intcount)))
                        End If
                    Else
                        sw.Write(String.Format("""{0}""", m_Datareader.Item(intcount)))
                    End If
4

2 回答 2

0

您可以覆盖当前线程的默认区域性以指定所需的默认 DateTime 格式:

    Dim c As CultureInfo = New CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name)
    c.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"
    c.DateTimeFormat.LongTimePattern = "HH:mm:ss.fff"
    System.Threading.Thread.CurrentThread.CurrentCulture = c
于 2013-03-07T19:56:56.917 回答
0

利用DateTime.TryParse

string input = "2000-02-02";
    DateTime dateTime;
    if (DateTime.TryParse(input, out dateTime))
    {
        Console.WriteLine(dateTime);
    }

更多示例

于 2013-03-07T19:27:32.787 回答