Vb.Net和CSV文件

时间:2012-03-31 13:58:33

标签: vb.net oracle10g

我有一个VB.Net应用程序,应该允许用户将CSV文件导入数据网格(它确实如此),然后将这些行更新为Oracle中的表。

到目前为止,这是我所拥有的,但它似乎无法正常工作。

Private Sub Update_Click(ByVal sender As System.Object, ByVal e As
   System.EventArgs) Handles Update.Click

    MsgBox("Saving...")

    Dim table As New DataTable()
    Dim BindingSource As New BindingSource()
    BindingSource.DataSource = table


    table.Columns.Add("ORDER_NO")
    table.Columns.Add("LINE_ITEM_NO")
    table.Columns.Add("CONTRACT")
    table.Columns.Add("PART_NO")
    table.Columns.Add("QTY_REQUIRED")
    table.Columns.Add("QTY_PER_ASSEMBLY")
    table.Columns.Add("RELEASE_NO")
    table.Columns.Add("SEQUENCE_NO")
    table.Columns.Add("ORDER_CODE")
    table.Columns.Add("PART_OWNERSHIP")

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

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


    parser.ReadLine()



Dim sConnectionString As String = "Data
Source=MYSERVER.COM;User ID=MYNAME;Password=MYPASSWD;"

Dim strSql As String = "INSERT INTO SHOP_MATERIAL_ALLOC_TAB(ORDER_NO,
LINE_ITEM_NO, CONTRACT, PART_NO, QTY_REQUIRED, QTY_PER_ASSEMBLY,
RELEASE_NO,SEQUENCE_NO,ORDER_CODE,PART_OWNERSHIP) VALUES (@ORDER_NO,
@LINE_ITEM_NO,@CONTRACT,@PART_NO,@QTY_REQUIRED,@QTY_PER_ASSEMBLY,@RELEASE_NO,@SEQUENCE_NO,@ORDER_CODE,@PART_OWNERSHIP)"


    Using conn As New OracleClient.OracleConnection(sConnectionString)

        Dim adapter As New OracleDataAdapter
        Dim cmd As New OracleClient.OracleCommand()
        cmd.Connection = conn

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

        '--cmd.ExecuteReader()
        cmd.Connection.Close()

        MsgBox("Saved! Kindly check your Shop order!")
        DataGridView1.DataSource = Nothing





    End Using

End Sub

现在我把它归结为在表中插入记录,但问题是它只解析行中的第一列。 因此,假设表中的所有6列都使用CSV中第一个字段的值进行更新。

    MsgBox("Saving...")


    Dim parser As New FileIO.TextFieldParser("C:\Documents and Settings\nUser\Desktop\solution.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.RowCount - 1


        Dim CurrentField = parser.ReadFields()
        '--parser.ReadLine()

Dim sConnectionString As String = "Data Source=MYSERVER.COM;User ID=MYUSER;Password=MYPASSWD;"

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

2 个答案:

答案 0 :(得分:0)

从未使用过oracle所以如果我是愚蠢的话,请不理我。

您是否尝试过conn.Connect()而不是cmd.Connection.Open()

答案 1 :(得分:0)

我不了解代码中创建DataTable并添加列的部分 它不会在任何地方使用,因此与您的问题无关 我们来看看与数据连接相关的代码

' If I remember well, the parameters in an oracle statement are prefixed by a ":" 
Dim strSql As String = "INSERT INTO SHOP_MATERIAL_ALLOC_TAB(ORDER_NO," +
                       "LINE_ITEM_NO, CONTRACT, PART_NO, QTY_REQUIRED, QTY_PER_ASSEMBLY, " +
                       "RELEASE_NO,SEQUENCE_NO,ORDER_CODE,PART_OWNERSHIP) VALUES (:ORDER_NO, " +
                       ":LINE_ITEM_NO,:CONTRACT,:PART_NO,:QTY_REQUIRED,:QTY_PER_ASSEMBLY, " +
                       ":RELEASE_NO,:SEQUENCE_NO,:ORDER_CODE,:PART_OWNERSHIP)" 

' Connection and Command are Disposable, so use `Using` around them
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 
     ' Now you need to add the parameters to your command
     ' One parameter for each column used in the insert statement
     ' I suppose that values for the paramenter are in the current line from your CSV
     cmd.Parameters.Add("ORDER_NO", OracleType.VarChar).Value = parser[index_of_orderNumber_Text]) 
     ' .... other parameters to be added....
     ' Now you can execute the INSERT statement directly with your command
     cmd.ExecuteNonQuery() 
 End Using 

而且,最后一点,Microsoft已弃用OracleClient命名空间,因此您应该尝试直接从Oracle替换ODP。