在文本文档中查找特定的文本行,并将接下来的37行文本插入到数据库中

时间:2011-02-05 22:03:00

标签: sql database vb.net text

直升机,      我有一个SQL数据库,50个文本文件和Visual Basic 2010 Premimum, 我需要在文本文件中找到特定的文本行,然后接下来的37行文本并将它们保存在我的数据库中。我需要建议,指出我正确的方向。此外,如果有人知道我可以使用的书以供将来参考,我们将不胜感激。

2 个答案:

答案 0 :(得分:1)

至于解析文件,逐行search files很容易:

Var i = 0
Var foundOnLineNumber = -1
For Each line In File.ReadAllLines("<file name here>")
    i = i + 1
    If foundOnLineNumber > 0 Then
        ' Add line to database
    Else If <criteria for finding "that" line> Then
        foundOnLineNumber = i
    End If
Next

我从来都不擅长VB(我通常做C#,所以这可能无法编译)。尝试找出您要查找的标准,并在上面的代码中替换它。 Here是VB.NET书籍的列表。找到一个涵盖ADO或其他一些数据库访问技术。我认为你最大的帮助就是简单地掌握VB语言和.NET所拥有的功能。

答案 1 :(得分:1)

Const textToFind As String = "a specific line of text"
Const maxLineCount As Int32 = 37
Dim allLines As New List(Of String)
Dim path As String = "c:\temp\MyTest.txt"

Try
   If File.Exists(path) Then
      File.Delete(path)
   End If

   Dim sw As StreamWriter = New StreamWriter(path)
   For i As Int32 = 1 To 100
      sw.WriteLine("This")
      sw.WriteLine("is some text")
       sw.WriteLine("to test")
       sw.WriteLine("Reading")
       sw.WriteLine("a specific line of text")
   Next
   sw.Close()

   Dim sr As StreamReader = New StreamReader(path)
   Dim lineCounter As Int32 = 0
   Dim lineFound As Int32 = -1
   Do While sr.Peek() >= 0
      Dim line As String = sr.ReadLine
      If lineFound = -1 Then
         If line.Equals(textToFind) Then
            lineFound = 0
         End If
      Else
         lineCounter += 1
         If lineCounter <= maxLineCount Then
            allLines.Add(line)
         Else
            Exit Do
         End If
      End If
   Loop
   sr.Close()
   ' save found lines to the database '
Catch ex As Exception
   Console.WriteLine("The process failed: {0}", ex.ToString())
End Try