[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2047
  • Last Modified:

To read pipe delimited file in/using VB.NET

I am looking for a logic in vb.net to read a pipe delimited file and load tables in sql.
we do not use DTS this has to be done vb.net. Any ideas will help me a lot.
0
florida73
Asked:
florida73
  • 2
2 Solutions
 
Fernando SotoCommented:
Hi florida73;

Well you could use the TextFieldParser file reader to read in a | delimited text file. Then formatting the fields to be used in the SQL insert stament. For example text to be inserted need ' and ' around them where integers do not. Once the data is in the right format it is just a matter of executing the SQL commands.

Fernando
0
 
Shiroy011899Commented:
Try this code snippet. If you need more help, let us know.
        Dim lines As String()
        lines = System.IO.File.ReadAllLines("test.txt")
        For Each sLine As String In lines
            Dim sValues As String()
            sValues = sLine.Split("|")
            For Each sItem As String In sValues
                ' Put the items into the database 
            Next
        Next

Open in new window

0
 
Shiroy011899Commented:
Use the string MyInsertQuery as the command to execute against your database (i.e. the Query to use)
Do let us know if you need and help.

Please note: The file should be of the format:

Value1|Value2|Value3
Value1|Value2|Value3
Value1|Value2|Value3

Where the number of values in each row is the same, and the same as the number of columns in your database table.
        Dim lines As String()
        Dim MyInsertQuery As String
        lines = System.IO.File.ReadAllLines("test.txt")
        For Each sLine As String In lines
            Dim sValues As String()
            MyInsertQuery = "INSERT INTO [MYTABLENAME] VALUES ("
            sValues = sLine.Split("|")
            For Each sItem As String In sValues
                MyInsertQuery = MyInsertQuery & "'" & sItem & "',"
            Next
            If MyInsertQuery.EndsWith(",") Then
                MyInsertQuery = MyInsertQuery.Remove(MyInsertQuery.Length - 1)
            End If
            MyInsertQuery = MyInsertQuery & ")"
            ' Use MyInsertQuery as your SQL Statement to
            ' insert values into the database
        Next

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now