2012-05-02 93 views
0

我正在循環一個文本文件,然後讀取並解析每一行......然後插入到sql服務器中。問題是,我收到其他每一行。有什麼想法嗎?我的StreamReader ReadLine每隔一行讀取

我在這裏閱讀另一篇文章,是類似的,它說那個人兩次調用ReadLine ......這是有道理的。我無法找到它發生的地方。

Private Sub btnUpdateRSR_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdateRSR.Click 


     ' Get RSR File 
     lblStatus.Text = "Getting RSR File" 
     Application.DoEvents() 

     If chkDLRSR.Checked = True Then 
      ftpGetRSR() 
     End If 

     Application.DoEvents() 
     lblStatus.Text = "Got RSR File" 

     ' Whack existing data 

     killRSRData() 


     ' Run Update of Invnetory 

     Dim sCmd As New SqlCommand() 
     Dim fp As StreamReader 
     Dim MyFile As String = String.Empty 
     Dim dblRecordCount As Double 


     Try 
      'Open file with StreamReader 
      fp = File.OpenText("c:\ct_info\rsr.txt") 
     Catch err As Exception 
      lblStatus.Text = "File Read Failed! Reason: " & err.ToString() 
     End Try 
     sCmd.Connection = New System.Data.SqlClient.SqlConnection("Data Source=vortx-sql01.vortx.com;Initial Catalog=expresspolicesupply;Persist Security Info=True;User ID=expresspolicesupply;Password=blahblah") 
     sCmd.Connection.Open() 
     Dim strArr() As String 

     While String.IsNullOrEmpty(fp.ReadLine) = False ' fp.ReadLine.IsNullOrEmpty = False 
      MyFile = fp.ReadLine 


      strArr = MyFile.Split(";") 
      'Show what is in the second position of the array. 
      'MessageBox.Show(strArr(1)) 
      Try 
       Dim RSRstocknumber As String = strArr(0) 
       Dim UPCcode As String = strArr(1) 
       Dim ProductDescription As String = Replace(strArr(2), "'", """") 
       Dim DepartmentNumber As String = strArr(3) 
       Dim ManufacturerID As String = strArr(4) 
       Dim RetailPrice As String = strArr(5) 
       Dim RSRRegularPrice As String = strArr(6) 
       Dim Weight As String = strArr(7) 
       Dim InventoryQuantity As String = strArr(8) 
       Dim Model As String = Replace(strArr(9), "'", """") 
       Dim FullManufacturerName As String = Replace(strArr(10), "'", """") 
       Dim ManufacturerPartNo As String = strArr(11) 
       Dim AllocatedCloseoutDeleted As String = strArr(12) 
       Dim ExpandedProductDescription As String = Replace(strArr(13), "'", """") 
       Dim ImageName As String = strArr(14) 



       lblStatusPrevious.Text = "Previous one: " & lblStatus.Text 
       lblStatus.Text = strArr(0) 
       Application.DoEvents() 





       With sCmd 
        .CommandText = "INSERT into rsr (rsrstocknumber, upccode, productDescription, departmentnumber, ManufacturerID, RetailPrice, RSRRegularPrice, Weight, InventoryQuantity, Model, FullManufacturerName, ManufacturerPartNo, AllocatedCloseoutDeleted, ExpandedProductDescription, ImageName) " & _ 
         " Values('" & RSRstocknumber & "', '" & UPCcode & "', '" & ProductDescription & "', '" & DepartmentNumber & "', '" & ManufacturerID & "', '" & _ 
       RetailPrice & "', '" & RSRRegularPrice & "', '" & Weight & "', '" & InventoryQuantity & "', '" & Model & "', '" & FullManufacturerName & "', '" & ManufacturerPartNo & "', '" & _ 
       AllocatedCloseoutDeleted & "', '" & ExpandedProductDescription & "','" & ImageName & "');" 
        'MessageBox.Show(sCmd.CommandText.ToString) 
        .CommandType = CommandType.Text 
        .ExecuteNonQuery() 

        ' Update record counter 

        dblRecordCount = dblRecordCount + 1 
        lblRecordCount.Text = dblRecordCount 
        Application.DoEvents() 



       End With 
      Catch ex As Exception 
       lblErrorLabel.Text = "Error on thown on " & lblRecordCount.Text 
       'MessageBox.Show("Error occurred! Details: " & ex.Message) 
      End Try 
     End While 
     fp.Close()   'Close file with StreamReader 
     sCmd.Connection.Close() 

     lblStatus.Text = "Updating website for RSR" 
     lblStatusPrevious.Text = "" 

     spUpdateRSR() 



     lblStatus.Text = "Done" 
     lblStatusPrevious.Text = "" 


    End Sub 

回答

1

這是因爲你打電話ReadLine()兩次。

其他所有行被String.IsNullOrEmpty(fp.ReadLine) = False吞噬。

+0

我該如何解決?當文件爲空/ EOF時,我必須有一些方法來停止。 –

+0

@MaggieRalph你在文件中循環。當沒有更多的行可讀時,它將停止讀取行。你不必這樣檢查。 while(reader.ReadLine()!= null)或者您可以事先使用Peek()。 – Yatrix

1

您正在調用ReadLine兩次。

While String.IsNullOrEmpty(fp.ReadLine) = False ' fp.ReadLine.IsNullOrEmpty = False 
     MyFile = fp.ReadLine 

一旦在IsNullOrEmpty檢查和一次在MyFile = fp.ReadLine

1

此行是讀取文件,然後你讀下一個while循環:

While String.IsNullOrEmpty(fp.ReadLine) = False 

要調用它兩次:)

你需要用它來檢查它是否是文件的結尾:

While fp.EndOfStream = False