遍历XML并将内容添加到访问表

时间:2019-06-04 23:23:38

标签: xml vb.net ms-access

嘿,我有一个XML文件,我想在其中循环遍历每个元素(有些不包含atk,def,level的值,而另一些则不包含),并将每个元素的内容添加到访问表中。调试时不会弹出错误,但访问表仅包含第一个元素的内容。这是XML内容的示例:https://i.stack.imgur.com/HXCnR.png 这是功能代码:

        Dim conn As New OleDbConnection
        conn = New OleDbConnection
        dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
        Dim databasePath = "Data Source = FULL YUGIOH ACCESS DATABASE.accdb;"
        conn.ConnectionString = dbprovider & databasePath
        Dim ds As New DataSet
        Dim command As OleDbCommand
        ds.ReadXml("C:\Users\1964\Documents\Visual Studio 2015\Projects\YGO Access Test v10\bin\Debug\MyData.xml")
        Try
            conn.Open()
            command = New OleDbCommand()
            maxrows = dataSet.Tables(0).Rows.Count

                For Each DtRow As DataRow In ds.Tables(0).Rows
                If Dtrow("level") Is Nothing OrElse IsDBNull(Dtrow("level")) And Dtrow("atk") Is Nothing OrElse IsDBNull(Dtrow("atk")) And Dtrow("def") Is Nothing OrElse IsDBNull(Dtrow("def")) Then
                    command.Parameters.AddWithValue("@name", Convert.ToString(Dtrow("name")))
                    command.Parameters.AddWithValue("@type", Convert.ToString(Dtrow("type")))
                    command.Parameters.AddWithValue("@type", Convert.ToString(Dtrow("type")))
                    command.Parameters.AddWithValue("@desc", Convert.ToString(Dtrow("desc")))
                    command.Parameters.AddWithValue("@race", Convert.ToString(Dtrow("race")))
                    command.Parameters.AddWithValue("@image_url", Convert.ToString(Dtrow("image_url")))
                    command.Connection = conn
                    command.CommandText = ("INSERT INTO [YESSSS] ([name],[type],[desc],[race],[image_url]) " &
                                                 "VALUES (@name,@type,@desc,@race,@image_url)")
                    command.ExecuteNonQuery()
                Else
                    command.Parameters.AddWithValue("@name", Convert.ToString(DtRow("name")))
                    command.Parameters.AddWithValue("@type", Convert.ToString(DtRow("type")))
                    command.Parameters.AddWithValue("@desc", Convert.ToString(DtRow("desc")))
                    command.Parameters.AddWithValue("@race", Convert.ToString(DtRow("race")))
                    command.Parameters.AddWithValue("@image_url", Convert.ToString(DtRow("image_url")))
                    command.Parameters.AddWithValue("@atk", Convert.ToInt32(DtRow("atk")))
                    command.Parameters.AddWithValue("@def", Convert.ToInt32(DtRow("def")))
                    command.Parameters.AddWithValue("@level", Convert.ToInt32(DtRow("level")))
                    command.Connection = conn
                    command.CommandText = ("INSERT INTO [YESSSS] ([name],[type],[desc],[race],[image_url],[atk],[def],[level]) " &
                                                  "VALUES (@name,@type,@desc,@race,@image_url,@atk,@def,@level)")
                    command.ExecuteNonQuery()

                End If
Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

您知道如何遍历所有XML元素并将其内容添加到访问表中吗?谢谢

0 个答案:

没有答案