0

我正在遍历一个文本文件并读取然后解析每一行..然后,插入到 sql server 中。问题是,我每隔一条线。有什么想法吗?

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

3 回答 3

1

那是因为你打ReadLine()了两次电话。

每隔一行都会被String.IsNullOrEmpty(fp.ReadLine) = False.

于 2012-05-02T14:59:16.700 回答
1

您正在调用 ReadLine 两次。

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

一次IsNullOrEmpty检查一次MyFile = fp.ReadLine

于 2012-05-02T14:59:47.637 回答
1

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

While String.IsNullOrEmpty(fp.ReadLine) = False

你叫它两次:)

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

While fp.EndOfStream = False 
于 2012-05-02T15:00:00.093 回答