文件导入提示

时间:2012-04-16 11:35:03

标签: vb.net oracle

下面的代码应该是在导入行的情况下更新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

1 个答案:

答案 0 :(得分: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))

分享并享受。