I have the following procedure that queries a database for an extremely large dataset and writes it to a text file (the resulting .txt file should be, roughly, 2-3gb in size).
Sub DBExecuteQueryWriteToFile(ByVal strSQLQuery As String, ByVal FileName As String, Optional ByVal Delimiter As String = ",")
Dim cmd = New OracleCommand(strSQLQuery, conn)
Dim buffersize As Integer = 1024 * 1024
Dim x As Integer = 0
Dim CountRow As Integer = 0
If File.Exists(FileName) Then
File.Delete(FileName)
End If
Using FileObject As New FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, buffersize)
Using StreamWriterObj As New StreamWriter(FileObject)
Using Reader As OracleDataReader = cmd.ExecuteReader()
Dim FieldCount As Integer = Reader.FieldCount - 1
' Write the headers
StreamWriterObj.Write(Reader.GetName(0))
For i = 1 To FieldCount
StreamWriterObj.Write(Delimiter)
StreamWriterObj.Write(Reader.GetName(i))
Next
StreamWriterObj.WriteLine()
' Write the file
Do While Reader.Read()
StreamWriterObj.Write(Reader.Item(0))
For i = 1 To FieldCount
StreamWriterObj.Write(Delimiter)
StreamWriterObj.Write(Reader.Item(i))
Next
StreamWriterObj.WriteLine()
Loop
End Using
End Using
End Using
End Sub
Now my problem is that when I try and use it on a smaller dataset, it writes the text file perfectly, when I try and use this to write the large dataset, it takes a very long time (as I would expect) and then it ends up with a 0kb file.
I originally thought the problem had to do with querying such a large dataset and, so, I posted this question, but then was told that wasn't the issue. So I don't know if this is a limitation on windows file sizes (I'm using windows 7, 64-bit with 16GB RAM) or if my code is bad somewhere else.
I know I can accomplish my end result by breaking the query down to get smaller datatsets and to write each one to a separate file, but I would also like to know if my code somewhere is incorrect / could be made to work better.
Any thoughts / insights would be greatly appreciated. Also, even though this is VB code, I'm equally comfortable with VB / C# as a solution.
Thanks!!!