Sql 在文本文档中查找特定的文本行,然后将接下来的37行文本插入数据库
你好, 我有一个SQL数据库,50个文本文件和Visual Basic 2010 Premimum,Sql 在文本文档中查找特定的文本行,然后将接下来的37行文本插入数据库,sql,database,vb.net,text,Sql,Database,Vb.net,Text,你好, 我有一个SQL数据库,50个文本文件和Visual Basic 2010 Premimum, 我需要在文本文件中找到一行特定的文本,然后将接下来的37行文本保存到我的数据库中。我需要一些建议,为我指明正确的方向。另外,如果有人知道我可以在将来参考的一本书,我将不胜感激。至于解析文件,很容易逐行进行: Var i = 0 Var foundOnLineNumber = -1 For Each line In File.ReadAllLines("<file name here>
我需要在文本文件中找到一行特定的文本,然后将接下来的37行文本保存到我的数据库中。我需要一些建议,为我指明正确的方向。另外,如果有人知道我可以在将来参考的一本书,我将不胜感激。至于解析文件,很容易逐行进行:
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
Var i=0
Var foundOnLineNumber=-1
对于文件.ReadAllLines(“”)中的每一行
i=i+1
如果foundOnLineNumber>0,则
'将行添加到数据库
否则
foundOnLineNumber=i
如果结束
下一个
我从来都不擅长VB(我通常使用C语言,所以这可能无法编译)。试着找出您要寻找的标准,并替换上面代码中的标准。是VB.NET书籍的列表。查找一个包含ADO或其他数据库访问技术的数据库。我认为您最大的帮助将是很好地掌握VB语言和.NET所拥有的功能。Const textToFind As String=“一行特定的文本”
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
常量maxLineCount为Int32=37
将所有行标注为新列表(字符串)
Dim路径为String=“c:\temp\MyTest.txt”
尝试
如果File.Exists(path)存在,则
File.Delete(路径)
如果结束
作为StreamWriter的Dim sw=新StreamWriter(路径)
对于i作为Int32=1到100
sw.WriteLine(“本”)
sw.WriteLine(“是一些文本”)
sw.WriteLine(“待测试”)
西南书写线(“阅读”)
sw.WriteLine(“文本的特定行”)
下一个
sw.Close()
Dim sr As StreamReader=新StreamReader(路径)
尺寸线计数器为Int32=0
找到的尺寸线为Int32=-1
当sr.Peek()>=0时执行此操作
作为字符串的尺寸线=sr.ReadLine
如果lineFound=-1,则
如果line.Equals(textToFind)则
lineFound=0
如果结束
其他的
行计数器+=1
中频线路计数器