嘿,我几周前曾问过这个问题,反馈确实有帮助,但我仍在努力将XML文件的内容传输到访问表中。该代码的错误是:“输入字符串的格式不正确。”您有解决此问题的想法吗?
-<XML>
-<cards>
<name>Majespecter Toad - Ogama</name>
<type>Pendulum Effect Monster</type>
<desc>When this card is Normal or Special Summoned: You can Set 1 "Majespecter" Spell/Trap Card directly from your Deck, but it cannot activate this turn. You can only use this effect of "Majespecter Toad - Ogama" once per turn. Cannot be targeted or destroyed </desc>
<race>Spellcaster</race>
<image_url>https://storage.googleapis.com/ygoprodeck.com/pics/645794.jpg</image_url>
<atk>1300</atk>
<def>500</def>
<level>4</level>
</cards>
-<cards>
<name>Gladiator Beast Retiari</name>
<type>Effect Monster</type>
<desc>When this card is Special Summoned by the effect of a "Gladiator Beast" monster, you can remove from play 1 card from your opponent's Graveyard. At the end of the Battle Phase, if this card attacked or was attacked, you can return it to the Deck to Specia</desc>
<race>Aqua</race>
<image_url>https://storage.googleapis.com/ygoprodeck.com/pics/612115.jpg</image_url>
<atk>1200</atk>
<def>800</def>
<level>3</level>
</cards>
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
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 command As OleDbCommand
Try
Dim f As XmlReader = XmlReader.Create("C:\Users\1964\Documents\Visual Studio 2015\Projects\YGO Access Test v10\bin\Debug\MyData.xml", New XmlReaderSettings())
Dim ds As New DataSet
ds.ReadXml(f)
conn.Open()
command = New OleDbCommand()
For Each row As DataRow In ds.Tables(0).Rows
Dim name As String = row.Item("name").ToString
Dim type As String = row.Item("type").ToString
Dim desc As String = row.Item("desc").ToString
Dim race As String = row.Item("race").ToString
Dim image_url As String = row.Item("image_url").ToString
Dim atk As String = row.Item("atk").ToString
Dim def As String = row.Item("def").ToString
Dim level As String = row.Item("level").ToString
command.Parameters.AddWithValue("@name", Convert.ToString(name))
command.Parameters.AddWithValue("@type", Convert.ToString(type))
command.Parameters.AddWithValue("@desc", Convert.ToString(desc))
command.Parameters.AddWithValue("@race", Convert.ToString(race))
command.Parameters.AddWithValue("@image_url", Convert.ToString(image_url))
command.Parameters.AddWithValue("@atk", Convert.ToInt32(atk))
command.Parameters.AddWithValue("@def", Convert.ToInt32(def))
command.Parameters.AddWithValue("@level", Convert.ToInt32(level))
command.Connection = conn
command.CommandText = "insert into [" & decknametxt.Text & "] (name, type, [desc], race, image_url, atk, def, [level]) values(@name, @type, @desc, @race, @image_url, @atk, @def, @level)"
command.ExecuteNonQuery()
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
答案 0 :(得分:0)
假设您的XML实际上是这样的
<XML>
<cards>
<name>Majespecter Toad - Ogama</name>
<type>Pendulum Effect Monster</type>
<desc>When this card is Normal or Special Summoned: You can Set 1 "Majespecter" Spell/Trap Card directly from your Deck, but it cannot activate this turn. You can only use this effect of "Majespecter Toad - Ogama" once per turn. Cannot be targeted or destroyed </desc>
<race>Spellcaster</race>
<image_url>https://storage.googleapis.com/ygoprodeck.com/pics/645794.jpg</image_url>
<atk>1300</atk>
<def>500</def>
<level>4</level>
</cards>
<cards>
<name>Gladiator Beast Retiari</name>
<type>Effect Monster</type>
<desc>When this card is Special Summoned by the effect of a "Gladiator Beast" monster, you can remove from play 1 card from your opponent's Graveyard. At the end of the Battle Phase, if this card attacked or was attacked, you can return it to the Deck to Specia</desc>
<race>Aqua</race>
<image_url>https://storage.googleapis.com/ygoprodeck.com/pics/612115.jpg</image_url>
<atk>1200</atk>
<def>800</def>
<level>3</level>
</cards>
</XML>
存储在文件系统上,则不需要XmlReader。只需向第一个ds.ReadXML arg提供文件路径。
Dim ds As New DataSet
ds.ReadXml("c:\data\test.xml")
Using AccessConn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\DATA\AccessDB.accdb")
AccessConn.Open()
For Each DtRow As DataRow In ds.Tables(0).Rows
Using Table1Cmd As New OleDb.OleDbCommand("INSERT INTO TABLE1 ([name],[type],[desc],[race],[image_url],[atk],[def],[level]) " &
"VALUES (@name,@type,@desc,@race,@image_url,@atk,@def,@level)", AccessConn)
Table1Cmd.Parameters.Add("@name", OleDb.OleDbType.VarChar).Value = Convert.ToString(DtRow("Name"))
Table1Cmd.Parameters.Add("@type", OleDb.OleDbType.VarChar).Value = Convert.ToString(DtRow("Type"))
Table1Cmd.Parameters.Add("@desc", OleDb.OleDbType.VarChar).Value = Convert.ToString(DtRow("Desc"))
Table1Cmd.Parameters.Add("@race", OleDb.OleDbType.VarChar).Value = Convert.ToString(DtRow("Race"))
Table1Cmd.Parameters.Add("@image_url", OleDb.OleDbType.VarChar).Value = Convert.ToString(DtRow("image_url"))
Table1Cmd.Parameters.Add("@atk", OleDb.OleDbType.VarChar).Value = Convert.ToString(DtRow("atk"))
Table1Cmd.Parameters.Add("@def", OleDb.OleDbType.VarChar).Value = Convert.ToString(DtRow("def"))
Table1Cmd.Parameters.Add("@level", OleDb.OleDbType.VarChar).Value = Convert.ToString(DtRow("level"))
Table1Cmd.ExecuteNonQuery()
End Using
Next
End Using