472,096 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Simple VB6 read and write converted to vb.net? How to Read CSV????

I think VB.NET drives some people crazy because some simple VB6 things
seem so hard.

Here is some VB6 code:

'Write CSV File
open "c:\test.csv" for output as #1
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
Close#1

'Read CSV File
open "c:\test.csv" for input as #1
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
Close#1

Works perfect and real easy. I can also open up the file in excel.

In VB.NET I can write the file the same way with:

FileOpen(1, "c:\testfile2.csv", OpenMode.Output)
WriteLine(1, "1", "2", "3", "4")
WriteLine(1, "2", "2", "3", "4")
WriteLine(1, "3", "2", "3", "4")
FileClose(1)

Now, how can I easily read the file from VB.NET into variables???? It
is driving me crazy and there seems to be about 10 different ways but
I want a simple CSV read routine. THANKS for any help.

Sheila

Sep 4 '07 #1
5 3110
vb.net is high Tech.
<ne**********@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>I think VB.NET drives some people crazy because some simple VB6 things
seem so hard.

Here is some VB6 code:

'Write CSV File
open "c:\test.csv" for output as #1
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
Close#1

'Read CSV File
open "c:\test.csv" for input as #1
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
Close#1

Works perfect and real easy. I can also open up the file in excel.

In VB.NET I can write the file the same way with:

FileOpen(1, "c:\testfile2.csv", OpenMode.Output)
WriteLine(1, "1", "2", "3", "4")
WriteLine(1, "2", "2", "3", "4")
WriteLine(1, "3", "2", "3", "4")
FileClose(1)

Now, how can I easily read the file from VB.NET into variables???? It
is driving me crazy and there seems to be about 10 different ways but
I want a simple CSV read routine. THANKS for any help.

Sheila

Sep 4 '07 #2
Your VB6 reader has a built in assumption about the number of columns in a
row of data. For example, your code needs to change if you want more data.
In general I would read the lines into an array of lines with
File.ReadAllLines, then split each row into colums with the string.Split()
method. If you know there are a lot of lines, then maybe reading one line at
a time will suffice.

"ne**********@gmail.com" wrote:
I think VB.NET drives some people crazy because some simple VB6 things
seem so hard.

Here is some VB6 code:

'Write CSV File
open "c:\test.csv" for output as #1
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
Close#1

'Read CSV File
open "c:\test.csv" for input as #1
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
Close#1

Works perfect and real easy. I can also open up the file in excel.

In VB.NET I can write the file the same way with:

FileOpen(1, "c:\testfile2.csv", OpenMode.Output)
WriteLine(1, "1", "2", "3", "4")
WriteLine(1, "2", "2", "3", "4")
WriteLine(1, "3", "2", "3", "4")
FileClose(1)

Now, how can I easily read the file from VB.NET into variables???? It
is driving me crazy and there seems to be about 10 different ways but
I want a simple CSV read routine. THANKS for any help.

Sheila

Sep 5 '07 #3
"Sambantham Kuppusamy" <sa*********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
vb.net is high Tech.
Is that sarcasm or an interesting variation on Ad Hominem (Ad Techinem?) ?

:^)

There's got to be a simpler way. What about "stream" reading. Also, people
have been writing header analysis routines for decades - I thought by now
header analysis would be implicit in any table reading
method/function/statement in a modern programming language...?

--
Timothy Casey GPEMC! >11950 is the nu****@fieldcraft.com.au 2email
Terms & conditions apply. See www.fieldcraft.biz/GPEMC
www.fieldcraft.biz & www.speed-reading-comprehension.com
www.geologist-1011.com & www.web-design-1011.com
Sep 5 '07 #4
Did you try it with AdoNet, that is a simple method and less clumsy (read
easily giving errors) as reading it as a kind of text file.

http://www.vb-tips.com/CSVDataSet.aspx

Cor
<ne**********@gmail.comschreef in bericht
news:11**********************@o80g2000hse.googlegr oups.com...
>I think VB.NET drives some people crazy because some simple VB6 things
seem so hard.

Here is some VB6 code:

'Write CSV File
open "c:\test.csv" for output as #1
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
Close#1

'Read CSV File
open "c:\test.csv" for input as #1
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
Close#1

Works perfect and real easy. I can also open up the file in excel.

In VB.NET I can write the file the same way with:

FileOpen(1, "c:\testfile2.csv", OpenMode.Output)
WriteLine(1, "1", "2", "3", "4")
WriteLine(1, "2", "2", "3", "4")
WriteLine(1, "3", "2", "3", "4")
FileClose(1)

Now, how can I easily read the file from VB.NET into variables???? It
is driving me crazy and there seems to be about 10 different ways but
I want a simple CSV read routine. THANKS for any help.

Sheila
Sep 5 '07 #5
Hi,
I use this simple and convenient class.
============================================
Imports System.Collections.Generic
Imports System.IO

Public Class CSVParser

#Region "Local Variables"
Protected m_fields As New List(Of String)
Protected m_separator As String = String.Empty
Protected m_stream As System.IO.StreamReader = Nothing
Protected m_names As New Dictionary(Of String, Int32)
#End Region
#Region "Ctors"
Public Sub New(ByVal PathName As String, Optional ByVal
Separator As String = ",")
m_separator = Separator
If System.IO.File.Exists(PathName) Then
m_stream = New System.IO.StreamReader(PathName)
Else
Throw New Exception("File not found")
End If
End Sub
#End Region

#Region "Methods"
Public Sub Close()
m_stream.Close()
m_stream = Nothing
End Sub

Public Sub ResetNames()
m_names.Clear()
End Sub
' associate field with name
Public Sub SetName(ByVal index As Int32, ByVal FieldName As
String)
If FieldName.Length 0 Then
If m_names.ContainsKey(FieldName) Then
Throw New Exception("Duplicate Name " & FieldName)
Else
m_names.Add(FieldName, index)
End If
Else
Throw New Exception("Empty Name Supplied")
End If
End Sub
Public Function ReadLine() As Boolean
m_fields.Clear()
If m_stream IsNot Nothing AndAlso Not m_stream.EndOfStream
Then
Dim buffer As String = m_stream.ReadLine()
If buffer.Length 0 Then
Parse(buffer)
Return True
End If
End If
Return False
End Function
Protected Sub Parse(ByVal buffer As String)
m_fields.AddRange(buffer.Split(New String() {m_separator},
StringSplitOptions.None))
End Sub
Public Function Count() As Int32
Return m_fields.Count
End Function
' no out of range exceptions
Public Overloads Function GetValue(ByVal index As Int32) As
String
If index >= 0 AndAlso index < m_fields.Count Then
Return m_fields(index)
End If
Return String.Empty
End Function

Public Overloads Function GetValue(ByVal name As String) As
String
Dim index As Int32
If m_names.TryGetValue(name, index) Then
Return GetValue(index)
End If
Throw New Exception("Invalid Field Name " & name.ToUpper)
End Function
#End Region

End Class

Public Class CSVFNParser : Inherits CSVParser
#Region "Ctors"
Public Sub New(ByVal PathName As String, Optional ByVal
Separator As String = ",")
MyBase.New(PathName, Separator)
Initialize()
End Sub

Protected Sub Initialize()
If ReadLine() Then
Dim N As Int32 = 0
For Each s As String In m_fields
Dim FieldName As String = s.ToUpper
If m_names.ContainsKey(FieldName) Then
Throw New Exception("Duplicate Name " &
FieldName)
Else
m_names.Add(FieldName, N)
End If
N += 1
Next
End If
If m_names.Count = 0 Then
Throw New Exception("No Field Names in CSV")
End If
End Sub
#End Region

End Class

----------------------------------

And sample program:

Sub Main()

Dim csv As New CSVParser("C:\blablabla.csv", ",")
While csv.ReadLine()
Dim N As Int32 = csv.Count
Console.WriteLine("There are {0} fields in this line", N)
For j as Int32 = 0 To N-1
If csv.GetValue(j).Length 0 Then
Console.WriteLine("[{0}]", csv.GetValue(j))
End If
Next

Console.WriteLine("---------------------------------------------------")
End While
Console.ReadLine()
End Sub

HTH
Serge
http://www.sergejusz.com

Sep 5 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Batista, Facundo | last post: by
7 posts views Thread by War Eagle | last post: by
1 post views Thread by oni | last post: by
3 posts views Thread by jamal | last post: by
6 posts views Thread by cdecarlo | last post: by
24 posts views Thread by Michael | last post: by
2 posts views Thread by Kevin Ar18 | last post: by
6 posts views Thread by arnuld | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.