473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Get text file content into SQL table

Hi

My text file varies in size on each occasion it is processed (i.e different
number of lines)
Contents are coma separated, giving data for each column in my sql table
(number of columns is constant)

My problem is not knowing how many lines are in my file.

I want to use this

"fileopen(1, filename, openmode.binary)"
myrecord = lineinput(1)
dim x = split(myrecord, ",")

Then, while looping through each line of the file, use the x array values to
populate sql parameter for use in a parameterized insert command. Open my sql
connection, execute my insert command, close the connection and move onto the
next line. Works perfect on another file I have ..... but that file has a
header line when received which tells me how many records are in the file

so, how will I know the end point of the loop if I don't know the number of
lines in the file

or is there another way of acheiving the same thing with the text file
format I have

Regards

Michael Bond
Aug 23 '06 #1
9 4284

mabond wrote:
Hi

My text file varies in size on each occasion it is processed (i.e different
number of lines)
Contents are coma separated, giving data for each column in my sql table
(number of columns is constant)

My problem is not knowing how many lines are in my file.

I want to use this

"fileopen(1, filename, openmode.binary)"
myrecord = lineinput(1)
dim x = split(myrecord, ",")

Then, while looping through each line of the file, use the x array values to
populate sql parameter for use in a parameterized insert command. Open my sql
connection, execute my insert command, close the connection and move onto the
next line. Works perfect on another file I have ..... but that file has a
header line when received which tells me how many records are in the file

so, how will I know the end point of the loop if I don't know the number of
lines in the file

or is there another way of acheiving the same thing with the text file
format I have

Regards

Michael Bond
' 2003 syntax (2005 has a using statement that would make this a bit
cleaner)
Imports System.IO

Dim sr As StreamReader

Try
sr = new StreamReader (pathToFile)
Dim line As String = sr.ReadLine ()
While Not Line Is Nothing
' process record
line = sr.ReadLine ()
End While
Catch
' handle exception
Finally
sr.Close ()
End Try

Something like this should suffice... I seriously would avoid the
built in VB.NET functions for file io. I normally don't steer people
away from functionality that is built into VB.NET - but I make
exception for the file IO. It is slow - though you would notice it to
much with small files, but if there is any possiblity that this might
be a file of any substantial size, then get to know System.IO and
forget FileOpen and friends....

--
Tom Shelton

Aug 23 '06 #2
Tom

thanks for that, and the welcome advice. The file I refer to is not
particularly big, but the other file I referred to could have upwards of
30,000 lines. So I'll review my method in light of your advice.

Many thanks

Michael Bond

"Tom Shelton" wrote:
>
mabond wrote:
Hi

My text file varies in size on each occasion it is processed (i.e different
number of lines)
Contents are coma separated, giving data for each column in my sql table
(number of columns is constant)

My problem is not knowing how many lines are in my file.

I want to use this

"fileopen(1, filename, openmode.binary)"
myrecord = lineinput(1)
dim x = split(myrecord, ",")

Then, while looping through each line of the file, use the x array values to
populate sql parameter for use in a parameterized insert command. Open my sql
connection, execute my insert command, close the connection and move onto the
next line. Works perfect on another file I have ..... but that file has a
header line when received which tells me how many records are in the file

so, how will I know the end point of the loop if I don't know the number of
lines in the file

or is there another way of acheiving the same thing with the text file
format I have

Regards

Michael Bond

' 2003 syntax (2005 has a using statement that would make this a bit
cleaner)
Imports System.IO

Dim sr As StreamReader

Try
sr = new StreamReader (pathToFile)
Dim line As String = sr.ReadLine ()
While Not Line Is Nothing
' process record
line = sr.ReadLine ()
End While
Catch
' handle exception
Finally
sr.Close ()
End Try

Something like this should suffice... I seriously would avoid the
built in VB.NET functions for file io. I normally don't steer people
away from functionality that is built into VB.NET - but I make
exception for the file IO. It is slow - though you would notice it to
much with small files, but if there is any possiblity that this might
be a file of any substantial size, then get to know System.IO and
forget FileOpen and friends....

--
Tom Shelton

Aug 23 '06 #3
Hello mabond,

I agree wholly with Tom. Try to make your code as BCL-reliant as possible
and avoid language-specific code as much as possible.

Just to be compleate though, the answer to the question asked is:

Dim tFileID as integer=FreeFile
OpenFile (tFileID, filename, openmode.binary)
Do until EOF(tFileID)
lineinput..
Loop
FileClose(tFileID)
As I mentioned to another poster on Monday, comma-delimited files are best
read with ADO.

-Boo

Tom

thanks for that, and the welcome advice. The file I refer to is not
particularly big, but the other file I referred to could have upwards
of 30,000 lines. So I'll review my method in light of your advice.

Many thanks

Michael Bond

"Tom Shelton" wrote:
>mabond wrote:
>>Hi

My text file varies in size on each occasion it is processed (i.e
different
number of lines)
Contents are coma separated, giving data for each column in my sql
table
(number of columns is constant)
My problem is not knowing how many lines are in my file.

I want to use this

"fileopen(1, filename, openmode.binary)"
myrecord = lineinput(1)
dim x = split(myrecord, ",")
Then, while looping through each line of the file, use the x array
values to populate sql parameter for use in a parameterized insert
command. Open my sql connection, execute my insert command, close
the connection and move onto the next line. Works perfect on another
file I have ..... but that file has a header line when received
which tells me how many records are in the file

so, how will I know the end point of the loop if I don't know the
number of lines in the file

or is there another way of acheiving the same thing with the text
file format I have

Regards

Michael Bond
' 2003 syntax (2005 has a using statement that would make this a bit
cleaner)
Imports System.IO
Dim sr As StreamReader

Try
sr = new StreamReader (pathToFile)
Dim line As String = sr.ReadLine ()
While Not Line Is Nothing
' process record
line = sr.ReadLine ()
End While
Catch
' handle exception
Finally
sr.Close ()
End Try
Something like this should suffice... I seriously would avoid the
built in VB.NET functions for file io. I normally don't steer people
away from functionality that is built into VB.NET - but I make
exception for the file IO. It is slow - though you would notice it
to much with small files, but if there is any possiblity that this
might be a file of any substantial size, then get to know System.IO
and forget FileOpen and friends....

--
Tom Shelton

Aug 24 '06 #4
Boo

thanks, that gives me what I asked for, but I'm grateful to you both for
adding that little bit extra. Gives me a new direction to explore, especially
if it will save processing time in the long run

Appreciate all the help

Regards

Michael Bond

"GhostInAK" wrote:
Hello mabond,

I agree wholly with Tom. Try to make your code as BCL-reliant as possible
and avoid language-specific code as much as possible.

Just to be compleate though, the answer to the question asked is:

Dim tFileID as integer=FreeFile
OpenFile (tFileID, filename, openmode.binary)
Do until EOF(tFileID)
lineinput..
Loop
FileClose(tFileID)
As I mentioned to another poster on Monday, comma-delimited files are best
read with ADO.

-Boo

Tom

thanks for that, and the welcome advice. The file I refer to is not
particularly big, but the other file I referred to could have upwards
of 30,000 lines. So I'll review my method in light of your advice.

Many thanks

Michael Bond

"Tom Shelton" wrote:
mabond wrote:

Hi

My text file varies in size on each occasion it is processed (i.e
different
number of lines)
Contents are coma separated, giving data for each column in my sql
table
(number of columns is constant)
My problem is not knowing how many lines are in my file.

I want to use this

"fileopen(1, filename, openmode.binary)"
myrecord = lineinput(1)
dim x = split(myrecord, ",")
Then, while looping through each line of the file, use the x array
values to populate sql parameter for use in a parameterized insert
command. Open my sql connection, execute my insert command, close
the connection and move onto the next line. Works perfect on another
file I have ..... but that file has a header line when received
which tells me how many records are in the file

so, how will I know the end point of the loop if I don't know the
number of lines in the file

or is there another way of acheiving the same thing with the text
file format I have

Regards

Michael Bond

' 2003 syntax (2005 has a using statement that would make this a bit
cleaner)
Imports System.IO
Dim sr As StreamReader

Try
sr = new StreamReader (pathToFile)
Dim line As String = sr.ReadLine ()
While Not Line Is Nothing
' process record
line = sr.ReadLine ()
End While
Catch
' handle exception
Finally
sr.Close ()
End Try
Something like this should suffice... I seriously would avoid the
built in VB.NET functions for file io. I normally don't steer people
away from functionality that is built into VB.NET - but I make
exception for the file IO. It is slow - though you would notice it
to much with small files, but if there is any possiblity that this
might be a file of any substantial size, then get to know System.IO
and forget FileOpen and friends....

--
Tom Shelton


Aug 24 '06 #5
Tom

This is what I have put in place

Using sr As New StreamReader(pathtoFile)
Dim lineno As Integer = 0
Try
Dim line As String = sr.ReadLine()
While Not line Is Nothing
Dim x = Split(line, ",")
'my code to pass to sql table here
lineno += 1
line = sr.ReadLine()
End While
Catch e As Exception
MessageBox.Show(e.ToString)
Finally
sr.Close()
End Try
End Using

Wasn't sure about the correct usage of the "Using" statement but I think i
got it right.

Thanks for the help

Regards

Michael Bond

"Tom Shelton" wrote:
>
mabond wrote:
Hi

My text file varies in size on each occasion it is processed (i.e different
number of lines)
Contents are coma separated, giving data for each column in my sql table
(number of columns is constant)

My problem is not knowing how many lines are in my file.

I want to use this

"fileopen(1, filename, openmode.binary)"
myrecord = lineinput(1)
dim x = split(myrecord, ",")

Then, while looping through each line of the file, use the x array values to
populate sql parameter for use in a parameterized insert command. Open my sql
connection, execute my insert command, close the connection and move onto the
next line. Works perfect on another file I have ..... but that file has a
header line when received which tells me how many records are in the file

so, how will I know the end point of the loop if I don't know the number of
lines in the file

or is there another way of acheiving the same thing with the text file
format I have

Regards

Michael Bond

' 2003 syntax (2005 has a using statement that would make this a bit
cleaner)
Imports System.IO

Dim sr As StreamReader

Try
sr = new StreamReader (pathToFile)
Dim line As String = sr.ReadLine ()
While Not Line Is Nothing
' process record
line = sr.ReadLine ()
End While
Catch
' handle exception
Finally
sr.Close ()
End Try

Something like this should suffice... I seriously would avoid the
built in VB.NET functions for file io. I normally don't steer people
away from functionality that is built into VB.NET - but I make
exception for the file IO. It is slow - though you would notice it to
much with small files, but if there is any possiblity that this might
be a file of any substantial size, then get to know System.IO and
forget FileOpen and friends....

--
Tom Shelton

Aug 24 '06 #6

mabond wrote:
Tom

This is what I have put in place

Using sr As New StreamReader(pathtoFile)
Dim lineno As Integer = 0
Try
Dim line As String = sr.ReadLine()
While Not line Is Nothing
Dim x = Split(line, ",")
'my code to pass to sql table here
lineno += 1
line = sr.ReadLine()
End While
Catch e As Exception
MessageBox.Show(e.ToString)
Finally
sr.Close()
End Try
End Using

Wasn't sure about the correct usage of the "Using" statement but I think i
got it right.
Well, with the useing statement you don't have to call sr.Close at all.
It will be called for you when the using block exits. So, you it
would probably look like:

Try
Using sr As New StreamReader (pathToFile)
' do your stuff
End Using ' sr is closed here
Catch e As Exception
MessageBox.Show (e.ToString)
End Try

Anyway, it is really syntactic sugur for try/finally/end try, but it
makes for some cleaner code...

--
Tom Shelton

Aug 24 '06 #7

mabond wrote:
Tom

This is what I have put in place

Using sr As New StreamReader(pathtoFile)
Dim lineno As Integer = 0
Try
Dim line As String = sr.ReadLine()
While Not line Is Nothing
Dim x = Split(line, ",")
'my code to pass to sql table here
lineno += 1
line = sr.ReadLine()
End While
Catch e As Exception
MessageBox.Show(e.ToString)
Finally
sr.Close()
End Try
End Using

Wasn't sure about the correct usage of the "Using" statement but I think i
got it right.
Well, with the useing statement you don't have to call sr.Close at all.
It will be called for you when the using block exits. So, you it
would probably look like:

Try
Using sr As New StreamReader (pathToFile)
' do your stuff
End Using ' sr is closed here
Catch e As Exception
MessageBox.Show (e.ToString)
End Try

Anyway, it is really syntactic sugur for try/finally/end try, but it
makes for some cleaner code...

--
Tom Shelton

Aug 24 '06 #8
On Wed, 23 Aug 2006 15:02:01 -0700, mabond <ma****@discussions.microsoft.comwrote:

¤ Hi
¤
¤ My text file varies in size on each occasion it is processed (i.e different
¤ number of lines)
¤ Contents are coma separated, giving data for each column in my sql table
¤ (number of columns is constant)
¤
¤ My problem is not knowing how many lines are in my file.
¤
¤ I want to use this
¤
¤ "fileopen(1, filename, openmode.binary)"
¤ myrecord = lineinput(1)
¤ dim x = split(myrecord, ",")
¤
¤ Then, while looping through each line of the file, use the x array values to
¤ populate sql parameter for use in a parameterized insert command. Open my sql
¤ connection, execute my insert command, close the connection and move onto the
¤ next line. Works perfect on another file I have ..... but that file has a
¤ header line when received which tells me how many records are in the file
¤
¤ so, how will I know the end point of the loop if I don't know the number of
¤ lines in the file
¤
¤ or is there another way of acheiving the same thing with the text file
¤ format I have

Using a single SQL INSERT...SELECT statement to perform the import or BULK INSERT would probably be
much more efficient.

http://msdn2.microsoft.com/en-us/library/ms188365.aspx
Paul
~~~~
Microsoft MVP (Visual Basic)
Aug 24 '06 #9
Thanks Paul

My skills in SQL are pretty limited and I've only just managed to overcome
the difficulties associated with getting the correct date format into an
"insert" statement by using parameters. That's why I was doing it line by
line. The solutions to this particular question provided by Tom and Boo meet
my needs at present but I appreciate your observation that Bulk Insert is
something I should look at and I'll follow that up as part of my steep
learning curve. Thanks for the feedback and suggestion. The help from all
contributors, both to this question, and others I've posted is very much
appreciated.

Regards

Michael Bond

"Paul Clement" wrote:
On Wed, 23 Aug 2006 15:02:01 -0700, mabond <ma****@discussions.microsoft.comwrote:

¤ Hi
¤
¤ My text file varies in size on each occasion it is processed (i.e different
¤ number of lines)
¤ Contents are coma separated, giving data for each column in my sql table
¤ (number of columns is constant)
¤
¤ My problem is not knowing how many lines are in my file.
¤
¤ I want to use this
¤
¤ "fileopen(1, filename, openmode.binary)"
¤ myrecord = lineinput(1)
¤ dim x = split(myrecord, ",")
¤
¤ Then, while looping through each line of the file, use the x array values to
¤ populate sql parameter for use in a parameterized insert command. Open my sql
¤ connection, execute my insert command, close the connection and move onto the
¤ next line. Works perfect on another file I have ..... but that file has a
¤ header line when received which tells me how many records are in the file
¤
¤ so, how will I know the end point of the loop if I don't know the number of
¤ lines in the file
¤
¤ or is there another way of acheiving the same thing with the text file
¤ format I have

Using a single SQL INSERT...SELECT statement to perform the import or BULK INSERT would probably be
much more efficient.

http://msdn2.microsoft.com/en-us/library/ms188365.aspx
Paul
~~~~
Microsoft MVP (Visual Basic)
Aug 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML....
14
by: Reply Via Newsgroup | last post by:
Folks, Say I have a table, ten columns, ten rows - Each with a word in it. I want to change the values of some/all of the cells in the table via a hyperlink. How do I reference each cell and...
2
by: Tom Youngquist | last post by:
I am trying to download a text file that my .NET page has just created based on entered parameters on the web page. Everything seems to work and the file is created. I am using the following code...
4
by: moondaddy | last post by:
Is there a asp.net validator control that validates the length of the text being entered or does everyone just write jscript for this? -- moondaddy@nospam.com
10
by: Eric Lindsay | last post by:
This may be too far off topic, however I was looking at this page http://www.hixie.ch/advocacy/xhtml about XHTML problems by Ian Hickson. It is served as text/plain, according to Firefox...
7
by: hawat.thufir | last post by:
Given an xhtml file, how can I "export" the data to plain-text? That is, I want: google www.google.com Whereas, if I copy and paste what the browser shows, I lose the URL and end up with:...
13
by: =?Utf-8?B?S2VzdGZpZWxk?= | last post by:
Hi Our company has a .Net web service that, when called via asp.net web pages across our network works 100%! The problem is that when we try and call the web service from a remote machine, one...
4
cassbiz
by: cassbiz | last post by:
Could use some help here. This script is carrying over an image just fine but the text isn't coming over. can you see why it is not working???? from the form I want to carry over two lines of...
27
matheussousuke
by: matheussousuke | last post by:
I'm having trouble with e-mail sending, I mean, the website is suposed to send a confirmation email after sign up, but it gets like text plain instead of HTML May someone help me, please? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.