-
Dec 4th, 2014, 11:47 AM
#1
Thread Starter
Hyperactive Member
Convert not standard text file into SQL
I need to read a text file and then insert into a SQL server.
The text file is not in standard format as below.
ABC, 12345,
BBB,9988, PP
ABC, 556699,
CCC.8899,UU
It should be format like below:
ABC, 12345,BBB,9988, PP
ABC, 556699,CCC.8899,UU
All row is starting "ABC"
How to read this text file?
-
Dec 4th, 2014, 12:16 PM
#2
Re: Convert not standard text file into SQL
In what way is that not standard? Is it just that there are extra line breaks?
The file is CSV. The fact that every row starts with ABC is pretty useful, as it would allow you to identify when a row has been wrapped. What might be more difficult is that, in your example, there aren't a fixed number of columns. The first row has 5 while the second row has only 4. That might be just a typo, though, since you separate BBB from 9988 with a comma, whereas you separate CCC from 8899 with a period. If that is real, then that is going to cause more trouble than the line breaks.
What I would suggest is to use ReadAllLines to get the file into an array of strings, then iterate through that dealing with the extra line breaks. Normally, you could query the file directly, but since the file has extra line breaks, then it won't query quite right, so pulling it into an array of strings and fixing the array seems like the best solution. It all depends on whether there is a fixed number of columns, though.
My usual boring signature: Nothing
-
Dec 4th, 2014, 12:34 PM
#3
Re: Convert not standard text file into SQL
Just split by position of ABC, and split the splitted text by comma and insert.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|