问题
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:
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()
来源:https://stackoverflow.com/questions/8941276/text-file-handling-with-sql-database-visual-basic