Text File Handling with SQL Database - Visual Basic

南楼画角 提交于 2019-12-13 01:23:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!