0

下面的代码应该使用导入的行来更新 oracle 中的表。但它似乎只使用从 CSV 文件导入的第一列更新表中的所有列。我认为它是需要操纵/更改的“ReadFields”属性。请给我任何建议,它很紧急。

    MsgBox("Saving...")

    Dim parser As New FileIO.TextFieldParser("C:\Documents and Settings\test\Desktop\test.csv")

    parser.Delimiters = New String() {","} ' fields are separated by comma
    parser.HasFieldsEnclosedInQuotes = True
    parser.TrimWhiteSpace = True

    Dim i As Integer

    For i = 0 To DataGridView1.ColumnCount - 1


        **Dim CurrentField = parser.ReadFields()**



        Dim sConnectionString As String = "Data Source=TEST;User ID=TEST;Password=TEST;"

        Dim strSql As String = "INSERT INTO SHOP_MATERIAL_ALLOCT(ORDER_NO, LINE_ITEM_NO, CONTRACT, PART_NO, QTY_REQUIRED, QTY_PER_ASSEMBLY) VALUES (:ORDER_NO,:LINE_ITEM_NO,:CONTRACT,:PART_NO,:QTY_REQUIRED,:QTY_PER_ASSEMBLY)"




        Using conn As New OracleClient.OracleConnection(sConnectionString)
            Using cmd As New OracleClient.OracleCommand()


                Dim adapter As New OracleDataAdapter
                conn.Open()
                cmd.Connection = conn
                cmd.CommandText = strSql
                cmd.Parameters.AddWithValue("ORDER_NO", CurrentField(i))
                cmd.Parameters.AddWithValue("LINE_ITEM_NO", CurrentField(i))
                cmd.Parameters.AddWithValue("CONTRACT", CurrentField(i))
                cmd.Parameters.AddWithValue("PART_NO", CurrentField(i))
                cmd.Parameters.AddWithValue("QTY_REQUIRED", CurrentField(i))
                cmd.Parameters.AddWithValue("QTY_PER_ASSEMBLY", CurrentField(i))



                cmd.CommandText = strSql
                adapter.InsertCommand = New OracleCommand(strSql, conn)
                adapter.UpdateCommand = cmd
                'adapter.Update(table)
                cmd.ExecuteNonQuery()
                cmd.Connection.Close()

                DataGridView1.DataSource = Nothing
            End Using
        End Using
    Next

End Sub
4

1 回答 1

2

FileIO.TextFieldParser.ReadFields 方法解析输入字符串中的所有字段并将它们存储在一个数组中。在上述情况下,您应该遍历返回数组中的各个值,但您始终使用 CurrentField(i)。尝试使用以下内容替换上面的相关行:

cmd.Parameters.AddWithValue("ORDER_NO", CurrentField(0))
cmd.Parameters.AddWithValue("LINE_ITEM_NO", CurrentField(1))
cmd.Parameters.AddWithValue("CONTRACT", CurrentField(2))
cmd.Parameters.AddWithValue("PART_NO", CurrentField(3))
cmd.Parameters.AddWithValue("QTY_REQUIRED", CurrentField(4))
cmd.Parameters.AddWithValue("QTY_PER_ASSEMBLY", CurrentField(5))

分享和享受。

于 2012-04-16T11:47:06.467 回答