1

データリーダーをCSVに変換する機能があります。SQLはYYYY-MM-DDHH: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

この関数は、データリーダーをCSVストリームに変換します。別の関数では、このストリームを文字列に変換してファイルに書き込みます。

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

それが私の問題を理解するのに役立つことを願っています。

または、別の方法は、MemoryStreamをファイルに直接書き込むことです。私はそれを試しましたが、うまくいきませんでした。ありがとう!

UPDATE#2Romilの提案に基づいて私がしたことは次のとおりです。

   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.TryParse

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

その他の例

于 2013-03-07T19:27:32.787 に答える
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 に答える