我的StreamReader ReadLine正在读取所有其他行

时间:2012-05-02 14:56:48

标签: vb.net

我循环遍历文本文件并读取然后解析每一行..然后,插入到SQL服务器。问题是,我得到了所有其他方面。有什么想法吗?

我在这里读到了另一篇类似的帖子,它说那个人两次调用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

3 个答案:

答案 0 :(得分:1)

那是因为你要两次致电ReadLine()

String.IsNullOrEmpty(fp.ReadLine) = False吞下了其他每一行。

答案 1 :(得分:1)

您正在两次调用ReadLine。

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

进入IsNullOrEmpty支票,进入MyFile = fp.ReadLine

答案 2 :(得分:1)

此行正在读取文件,然后您在while循环中读取下一行:

While String.IsNullOrEmpty(fp.ReadLine) = False

你打电话两次:)

您需要使用它来检查它是否是文件的末尾:

While fp.EndOfStream = False