1

I have a text file contains delimited records.

1243;jhhf';982u4k;9u2349;huf8
kij;9238u;98ur23;jfwf;03i24

I need to replace the value of 4th part of every record with the value returned from SQL database (Select X from T where C='4Th part from the flatfile').

Regards,
SAnthosh.

1

1 Answer 1

1

Try this:

Dim newLines As List(Of String) = New List(Of String)
Dim sqlConn As New SqlConnection(connectionString)
Dim SQLCmd As New SqlCommand()
SQLCmd.Connection = sqlConn
Dim lines As String() = File.ReadAllLines(filename)
sqlConn.Open()
For Each line As String In lines
    Dim parts As String() = line.Split(";")
    SQLCmd.CommandText = "Select X from T where C=""" & parts(3) & """"
    Dim dr As SqlDataReader = SQLCmd.ExecuteReader
    While dr.Read()
        parts(3) = dr("X")
    End While
    newLines.Add(String.Join(";", parts))
Next
File.WriteAllLines(filename, newLines.ToArray())
sqlConn.Close()
Sign up to request clarification or add additional context in comments.

9 Comments

System.IndexOutOfRangeException was unhandled Message="Index was outside the bounds of the array."
@user1157902: this means that line.Split returned less than 4 parts, so your file is not the way you described I think....
A;B;C;160;D /n X;Y;Z;161;W /n P;Q;R;162;S/n A;B;C;163;D/n (4 lines ) This is the content. i'm just testing before implementing into the actual.
@Santhosh_ms3: I tried my code and it works for me; I edited it because datareader name was wrong, but I can't reproduce your error. Are you sure about "/n"? Is that a newline?
yes its(\n) a newline. Actually i got an error {"There is already an open DataReader associated with this Command which must be closed first."} so closd the Sqldr as SQLdr.close() . after that geting the exception "Index was outside the bounds of the array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.