I have a program that reads in a folder of files (4GB all together), indexes them, then adds them to a linked SQL database. This all occurs before the form even loads.
Now I wish to have another file containing one 13 digit number per line and query the SQL database from VB searching for each number to return which folder, file, and line number contains that number.
The method I have below works now, but it's slow. I need to find a much quicker way to go straight to the specific line in a file for my number and write a new line of text at that location.
Dim result = From n In System.IO.File.ReadLines("G:\USER\SearchThese.txt")
Select n.Substring(0, 13)
Dim MyFilePath As String
Dim linePos As String
Dim lines As String
'-- connection
Dim con As New SqlConnection(***MY SQL CONNECTION***)
Dim dataset As New DataSet
Dim datatable As DataTable
Dim dataadapter As New SqlDataAdapter
Dim sql As String
Dim i As Integer
'-- command
Dim cmd As New SqlCommand()
con.Open()
Using sw As New StreamWriter("G:\USER\TESTRUN1.txt")
For Each word As String In result
i = 0
sql = ("SELECT * FROM Test_Table WHERE DigNum = @word")
dataadapter = New SqlDataAdapter(sql, con)
dataadapter.SelectCommand.Parameters.AddWithValue("@word", word)
dataset = New DataSet()
dataadapter.Fill(dataset, "Test_Table")
While i < dataset.Tables("Test_Table").Rows.Count
linePos = dataset.Tables("Test_Table").Rows(i).Item(4).ToString()
MyFilePath = dataset.Tables("Test_Table").Rows(i).Item(1).ToString()
i += 1
Using sr As New StreamReader(MyFilePath)
For n As Integer = 1 To linePos
lines = sr.ReadLine
Next
sw.WriteLine(lines)
End Using
End While
Next
End Using
MsgBox("Complete!")
Like I said this works fine, but even searching for just 5 numbers takes fairly long. I'm guessing the streamreader that slows it down, but I don't know.
For anyone asking, my database table has these columns:
DigNum | FilPth | FilDte | DteAdd | LnePos