473,385 Members | 1,615 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,385 software developers and data experts.

read fixed column flat file into an SQLserver table

I need to parse a text file with fixed columns into a table. I have the file
spec (column positions) but don't know how to use it.
Thanks

Bill
Nov 21 '05 #1
2 3276
You probably want to use a StreamReader to read in the lines of text from
the fixed-width file into a string variable. Then create SQL Insert
statements that use the Substring() of the various lines as input. You can
also use DTS to Import the data from your fixed-width file via Enterprise
Manager (probably easier).

' open a streamreader above this (named sr in this example)
' Then we process each line
Dim sqlcon As New SqlConnection ("connection string")
sqlcon.Open()
Dim sqlcmd As New SqlCommand("INSERT INTO MyTable " + _
"(LastName, FirstName) " + _
"VALUES (@lastname, @firstname)", sqlcon)
sqlcmd.Parameters.Add("@lastname", SqlDbType.VarChar, 255)
sqlcmd.Parameters.Add("@firstname", SqlDbType.VarChar, 255)
Dim line As String
line = sr.ReadLine()
Do While Not (line Is Nothing)
' For example, assume last name is columns 1 - 25 and
' firstname is columns 26 - 50 of flat file
sqlcmd.Parameters("@lastname").Value = line.Substring(1, 25)
sqlcmd.Parameters("@firstname").Value = line.Substring(26, 25)
sqlcmd.ExecuteNonQuery()
line = sr.ReadLine()
Loop
sqlcmd.Dispose()
sqlcon.Dispose()
' Close your streamreader, etc. below

This is a really simplified example, but it gives you the basic idea.
Adding Try..Catch..Finally blocks and the StreamReader code is left as an
exercise for the reader. Also, if you're loading into an Access database,
you'll use the OleDbCommand and OleDbConnection objects instead of
SqlCommand and SqlConnection.

"Bill Nguyen" <bi*****************@jaco.com> wrote in message
news:OV****************@TK2MSFTNGP10.phx.gbl...
I need to parse a text file with fixed columns into a table. I have the
file spec (column positions) but don't know how to use it.
Thanks

Bill

Nov 21 '05 #2
Lookup BULK INSERT in the MSDN Library's Transaction-SQL Reference, it lets
you import data from user-defined files into a database table. This is
really a action you should do in Transaction-SQL and not in VB.NET.
"Bill Nguyen" <bi*****************@jaco.com> skrev i melding
news:OV****************@TK2MSFTNGP10.phx.gbl...
I need to parse a text file with fixed columns into a table. I have the
file spec (column positions) but don't know how to use it.
Thanks

Bill


Nov 21 '05 #3

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

Similar topics

4
by: Rob Freundlich | last post by:
I have some servlet-generated tabular data that I need to present, so I'm using an HTML Table. In some cases, it can be quite large. I'm flushing the servlet output every N lines to push the data...
3
by: Carmine | last post by:
I have to add a date column to a db2/zos table with a blob column. Can I rename the blob table to old, create a new table with the date column, then insert/select the data from the old table? ...
0
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
2
by: Brian Henry | last post by:
In C++ you could easily create a fixed with string by creating it with char(50) or so as an example. This made it easy to write data out to a flat file with a fixed width column. What is the best...
1
by: elena | last post by:
I have large flat file with fixed record lenght, i have to parse record and populate table with multi columns with the data, please any links or code snipets to this topic will be greatly...
5
by: SQLMan_25 | last post by:
Hi All, I am trying to create a user defined function that fetches the price of an item. I have written a scalar function that takes itemid and returns its price. Simple version of tables would...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
4
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.