0

私はテキストファイルをループして読み取り、各行を解析しています..次に、SQLサーバーに挿入しています。問題は、1行おきに取得していることです。何かご意見は?

ここで似たような別の投稿を読んだところ、その人が ReadLine を 2 回呼び出していたことがわかりました...これは理にかなっています。私はそれがどこで起こっているのかを見つけることができません。

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()2 回電話をかけているからです。

1 行おきに に飲み込まれString.IsNullOrEmpty(fp.ReadLine) = Falseます。

于 2012-05-02T14:59:16.700 に答える
1

ReadLine を 2 回呼び出しています。

 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

あなたはそれを2回呼び出しています:)

これを使用して、ファイルの終わりかどうかを確認する必要があります。

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