如何在MySql中从XML插入数据?

时间:2019-02-27 15:54:55

标签: mysql vb.net

实际上,我正在尝试在VB.NET中创建一个程序,该程序从文件夹中获取xml文件并将其数据设置为MySQL。

每个XML文件都有唯一的ID,为此文件将创建一个数据库和所有表。 在该文件夹中,我可以包含“ DC_001”,“ DF_001”,“ DC_002”之类的文件,这意味着必须将XML文件DC_001中的数据放置在表DC中的数据库001中。

实际上,我已经制定了为每个唯一ID及其表创建数据库的方法,但是由于我不太喜欢VB.NET,因此我需要一些有关如何将XML内的数据放入每个数据库的建议。

这是我创建数据库和表的方法。

    Dim PIVA As New ArrayList
    Dim conn As MySqlConnection = New MySqlConnection
    conn.ConnectionString = "server=127.0.0.1;user id=root; password=block; database=mysql"

    For Each fileName As String In Directory.EnumerateFileSystemEntries("C:\Users\imytyuk\Desktop\test")
        Dim file As String = Trim(EstrCampo("_", fileName, 1, 2))
        If Not PIVA.Contains(file) Then
            PIVA.Add(file)
        End If
    Next

    For Each iva In PIVA
        Dim cmd As MySqlCommand = New MySqlCommand("CREATE DATABASE `" & iva & "`", conn)
        Try
            conn.Open()
            Try
                cmd.ExecuteNonQuery()
            Catch ex As Exception
            End Try
            conn.Close()
            CreaTB(iva)
        Catch
        End Try
    Next

这就像可以构造XML文件一样

enter image description here

更新:

我正在尝试使用以下方法从XML插入数据,但出现异常情况是“行数与行1的值计数不符”。

代码如下:

Sub AddXML()
        For Each fileName As String In Directory.EnumerateFileSystemEntries("C:\Users\imytyuk\Desktop\test")
            databaseFilePut(fileName, "datacollect", Trim(EstrCampo("_", fileName, 1, 2)))
            File.Delete(fileName)
        Next

    End Sub

    Public Shared Sub databaseFilePut(ByVal varFilePath As String, ByVal table As String, ByVal db As String)
        Dim file() As Byte
        Dim stream = New FileStream(varFilePath, FileMode.Open, FileAccess.Read)
        Dim reader = New BinaryReader(stream)
        file = reader.ReadBytes(CType(stream.Length, Integer))
        Dim conn As MySqlConnection = New MySqlConnection
        conn.ConnectionString = "server=127.0.0.1;user id=root; password=block; database=" & db
        Dim cmd As MySqlCommand = New MySqlCommand("INSERT INTO " & table & " Values(@File)", conn)
        Try
            conn.Open()
            cmd.Parameters.Add("@File", MySqlDbType.VarBinary, file.Length).Value = file
            cmd.ExecuteNonQuery()
        Catch ex As Exception

        End Try

    End Sub

0 个答案:

没有答案