2011-02-05 10 views
0

Helo、 SQLデータベースと50個のテキストファイルとVisual Basic 2010 Premimumを持っています 特定のテキスト行をテキストファイルに保存してから、次の37行のテキストをデータベースに保存します。私は正しい方向に私を指すように助言が必要です。また、誰かが私が将来参考に使うことができる本を知っていれば、それは非常に高く評価されるだろう。ファイルを解析するようテキスト文書内の特定のテキスト行を検索し、次の37行のテキストをデータベースに挿入します。

答えて

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 
+0

ありがとうございます。 – robblot

1

、それはsearch filesライン・バイ・ラインに簡単です:私は(私は通常のC#を行いますので、これはコンパイルされない場合があります)VBで良い行ったことがない

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 

。上記のコードで、あなたが探している基準を見つけて置き換えてみてください。 HereはVB.NETの本のリストです。 ADOや他のデータベースアクセス技術をカバーするものを探します。あなたの最大の助けとなるのは、.NETが自由に使えるV​​B言語と機能を簡単に把握することだと思います。

+0

ありがとうございます – robblot

関連する問題