Sancler
asked on
StreamWriter - .Write or .WriteLine - a discrepancy?
The background to this question is here
https://www.experts-exchange.com/questions/22058379/Processing-multiple-textr-files-usiing-dataadapter.html
It explains - if any explanation is needed - why the task that is simplified in the code below might be carried out "for real".
This is VB.NET 2005.
A test file with these contents
1;Adam
2;Bill
3;Chris
4;Dave
saved in my case as "C:\Test\TestFile.txt". If you try this and change the path/name, you will need to change the references in both Button_Click subs below.
One form with two buttons on and one datagridview (called DGV). This code
Imports System.IO
Imports System.Data.OleDb
Public Class frmTest
Private Sub makeSchemaWithWrite(ByVal thePath As String, ByVal theFile As String, ByVal delimiter As Char, ByVal headers As Boolean)
Dim thisFile As String = thePath & "\schema.ini"
'Ensure any existing schema ini file is deleted
If My.Computer.FileSystem.Fil eExists(th isFile) Then
My.Computer.FileSystem.Del eteFile(th isFile)
End If
Dim f As New StreamWriter(thisFile)
Dim t As String = "[" & theFile & "]" & vbCrLf
t &= "Format=Delimited(;)" & vbCrLf
t &= "ColNameHeader = " & headers & vbCrLf
t &= "CharacterSet=ANSI" & vbCrLf
t &= "col1=ID Integer" & vbCrLf
t &= "col2=Name char width 10" & vbCrLf
f.Write(t)
f.Close()
f.Dispose()
End Sub
Private Sub makeSchemaWithWriteLine(By Val thePath As String, ByVal theFile As String, ByVal delimiter As Char, ByVal headers As Boolean)
Dim thisFile As String = thePath & "\schema.ini"
'Ensure any existing schema ini file is deleted
If My.Computer.FileSystem.Fil eExists(th isFile) Then
My.Computer.FileSystem.Del eteFile(th isFile)
End If
Dim f As StreamWriter
f = My.Computer.FileSystem.Ope nTextFileW riter(this File, False)
f.WriteLine("[" & theFile & "]")
f.WriteLine("Format=Delimi ted(;)")
f.WriteLine("ColNameHeader = " & headers)
f.WriteLine("CharacterSet= ANSI")
f.WriteLine("col1=ID Integer")
f.WriteLine("col2=Name char width 10")
f.Close()
f.Dispose()
End Sub
Private Sub LoadData(ByVal fname As String, ByVal useLines As Boolean)
If Not My.Computer.FileSystem.Fil eExists(fn ame) Then Exit Sub
Dim fiMyFile As FileInfo = New FileInfo(fname)
Dim thisPath As String = fiMyFile.DirectoryName
Dim sourceFile As String = fiMyFile.Name
If useLines Then
makeSchemaWithWriteLine(th isPath, sourceFile, ";", False)
Else
makeSchemaWithWrite(thisPa th, sourceFile, ";", False)
End If
Dim sConnectionString As String = "Provider=Microsoft.Jet.OL EDB.4.0;" & "Data Source=" & thisPath & ";Extended Properties=""Text"";"
Dim cConn As New OleDbConnection(sConnectio nString)
Dim sSQL As String = "SELECT * FROM " & sourceFile & " WHERE ID > 2"
Dim da As New OleDbDataAdapter(sSQL, cConn)
Dim dt As New DataTable
Try
da.Fill(dt)
DGV.DataSource = dt
Catch ex As Exception
MsgBox(ex.Message)
DGV.DataSource = Nothing
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
LoadData("C:\Test\TestFile .txt", True)
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
LoadData("C:\Test\TestFile .txt", False)
End Sub
End Class
The purpose of the program is to read the test file's contents, which are semicolon-delmited, using the Jet.OleDB Text engine and a schema.ini, into a datatable, hence datagridview. The schema.ini is written on the fly. Two methods of writing it are used. MessageBox code is included to show that in both cases the schema.ini file has been written. But when it gets to using that file, one works OK: the other doesn't.
My question is, why?
Roger
https://www.experts-exchange.com/questions/22058379/Processing-multiple-textr-files-usiing-dataadapter.html
It explains - if any explanation is needed - why the task that is simplified in the code below might be carried out "for real".
This is VB.NET 2005.
A test file with these contents
1;Adam
2;Bill
3;Chris
4;Dave
saved in my case as "C:\Test\TestFile.txt". If you try this and change the path/name, you will need to change the references in both Button_Click subs below.
One form with two buttons on and one datagridview (called DGV). This code
Imports System.IO
Imports System.Data.OleDb
Public Class frmTest
Private Sub makeSchemaWithWrite(ByVal thePath As String, ByVal theFile As String, ByVal delimiter As Char, ByVal headers As Boolean)
Dim thisFile As String = thePath & "\schema.ini"
'Ensure any existing schema ini file is deleted
If My.Computer.FileSystem.Fil
My.Computer.FileSystem.Del
End If
Dim f As New StreamWriter(thisFile)
Dim t As String = "[" & theFile & "]" & vbCrLf
t &= "Format=Delimited(;)" & vbCrLf
t &= "ColNameHeader = " & headers & vbCrLf
t &= "CharacterSet=ANSI" & vbCrLf
t &= "col1=ID Integer" & vbCrLf
t &= "col2=Name char width 10" & vbCrLf
f.Write(t)
f.Close()
f.Dispose()
End Sub
Private Sub makeSchemaWithWriteLine(By
Dim thisFile As String = thePath & "\schema.ini"
'Ensure any existing schema ini file is deleted
If My.Computer.FileSystem.Fil
My.Computer.FileSystem.Del
End If
Dim f As StreamWriter
f = My.Computer.FileSystem.Ope
f.WriteLine("[" & theFile & "]")
f.WriteLine("Format=Delimi
f.WriteLine("ColNameHeader
f.WriteLine("CharacterSet=
f.WriteLine("col1=ID Integer")
f.WriteLine("col2=Name char width 10")
f.Close()
f.Dispose()
End Sub
Private Sub LoadData(ByVal fname As String, ByVal useLines As Boolean)
If Not My.Computer.FileSystem.Fil
Dim fiMyFile As FileInfo = New FileInfo(fname)
Dim thisPath As String = fiMyFile.DirectoryName
Dim sourceFile As String = fiMyFile.Name
If useLines Then
makeSchemaWithWriteLine(th
Else
makeSchemaWithWrite(thisPa
End If
Dim sConnectionString As String = "Provider=Microsoft.Jet.OL
Dim cConn As New OleDbConnection(sConnectio
Dim sSQL As String = "SELECT * FROM " & sourceFile & " WHERE ID > 2"
Dim da As New OleDbDataAdapter(sSQL, cConn)
Dim dt As New DataTable
Try
da.Fill(dt)
DGV.DataSource = dt
Catch ex As Exception
MsgBox(ex.Message)
DGV.DataSource = Nothing
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
LoadData("C:\Test\TestFile
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
LoadData("C:\Test\TestFile
End Sub
End Class
The purpose of the program is to read the test file's contents, which are semicolon-delmited, using the Jet.OleDB Text engine and a schema.ini, into a datatable, hence datagridview. The schema.ini is written on the fly. Two methods of writing it are used. MessageBox code is included to show that in both cases the schema.ini file has been written. But when it gets to using that file, one works OK: the other doesn't.
My question is, why?
Roger
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Mike
Thanks. I wondered about that and reckoned to check - not with the msdn docs, but by examining the files at byte level. But, I have to admit, it was quite late and I probably put a wrong file through the loop. I'll have a crack at it again with that in mind. But I'm on something else now, so it'll be a few hours. Watch this space.
Roger
Thanks. I wondered about that and reckoned to check - not with the msdn docs, but by examining the files at byte level. But, I have to admit, it was quite late and I probably put a wrong file through the loop. I'll have a crack at it again with that in mind. But I'm on something else now, so it'll be a few hours. Watch this space.
Roger
Roger,
So others can benefit as well...
Which one works?
(A) "UTF-8 encoding without a Byte-Order Mark (BOM)"
via New StreamWriter(thisFile)
(B) ASCII
via My.Computer.FileSystem.Ope nTextFileW riter(this File, False)
So others can benefit as well...
Which one works?
(A) "UTF-8 encoding without a Byte-Order Mark (BOM)"
via New StreamWriter(thisFile)
(B) ASCII
via My.Computer.FileSystem.Ope
ASKER
Mike
That's it. I re-tried my byte-reads and got the BOM on the one that didn't work but not on the other. Anyway, although the actual problem was resolved at the time, it's nice now to know quite why.
Thanks
Roger
That's it. I re-tried my byte-reads and got the BOM on the one that didn't work but not on the other. Anyway, although the actual problem was resolved at the time, it's nice now to know quite why.
Thanks
Roger
ASKER
Cross post. Just for clarification. fFor this purpose we MUST use New StreamWriter(thisFile) and NOT My.Computer.FileSystem.Ope nTextFileW riter(this File, False)
Roger
Roger
=)
ASKER
Dim sr As New StreamReader(thisPath & "\schema.ini")
MsgBox(sr.ReadToEnd)
sr.Close()
should go immediately after
If useLines Then
makeSchemaWithWriteLine(th
Else
makeSchemaWithWrite(thisPa
End If
in the LoadData sub
Roger