2012-05-02 5 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을 두 번 호출하고 있습니다. 일단 IsNullOrEmpty 검사에서 한 번

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

MyFile = fp.ReadLine

1

에이 줄은 파일을 읽고, 다음은 while 루프의 다음 하나 읽어

While String.IsNullOrEmpty(fp.ReadLine) = False 

당신은 그것을 호출을 두 번 :

파일의 끝인지 여부를 확인하려면이 파일을 사용해야합니다.

While fp.EndOfStream = False 
관련 문제