Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use Regex?

Status
Not open for further replies.

tsp1lrk72

IS-IT--Management
Feb 25, 2008
100
US
Hi- I'm trying to use a regex to parse a text file and then insert it into a sql db-- when I run this it just keeps adding null records to the db, something is wrong... is there a better way?

Code:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strSQL As String
        Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
        Dim objCmd As SqlCommand
        Dim sr As New System.IO.StreamReader("c:\Sundaytest.txt")
        Dim pattern As String = "^(?<first>.{35})(?<second>.{30})(?<third>.{15})(?<fourth>.{11})(?<fifth>.{35})(?<sixth>.{14})(?<seventh>.+)$"

        Dim re As New System.Text.RegularExpressions.Regex(pattern)
        Dim ma As System.Text.RegularExpressions.Match = re.Match(sr.ReadLine())

        Do While sr.Peek <> -1
            strSQL = "insert into InovisInt_Lisa (ISAID, TPName)values(@ISAID, @TPName)"
            objCmd = New SqlCommand(strSQL, conn)
            objCmd.Parameters.Add("@ISAID", ma.Groups("first").Value.TrimEnd())
            objCmd.Parameters.Add("@TPName", ma.Groups("second").Value.TrimEnd())
            conn.Open()
            objCmd.ExecuteNonQuery()
            conn.Close()
        Loop
        sr.Close()
    End Sub

any advice apppreciated
vb.net 2003-- i posted there too...
 
You'll have to trace through your code with the debugger to see where what you are expecting, is failing.
 
I read about the bulk insert, but we wanted to do this because we have to parse the fixed width file, that's why I was using the regex, but I'm having an issue getting this into a SQL table...Ugh!! Any ideas? or is this totally wrong?

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top