From XML database to ACCESS database-VBForums
Results 1 to 2 of 2

Thread: From XML database to ACCESS database

  1. #1

    Thread Starter
    Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    59

    From XML database to ACCESS database

    Goodmorning everyone, I am not satisfied with this solution, can you suggest something better ?
    Code:
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim databaseName As String = "F:\Documents\Visual Studio 2015\Projects\xml.mdb"
            Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & databaseName)
            Dim ds As New Data.DataSet
            ds.ReadXml("F:\Documenti\Visual Studio 2015\Projects\test.xml")
            Dim dstable As DataTable = ds.Tables("Mytable")
            DataGridView1.DataSource = dstable
            ' ----- Create Access Database file using ADOX
            Dim cat As ADOX.Catalog = New ADOX.Catalog()
            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & databaseName & ";Jet OLEDB:Engine Type=5")
            MessageBox.Show("Database Created Successfully")
    
            con.Open()
            Dim sql As String = "Create Table tab1 ("
            For Each column As DataColumn In dstable.Columns
                sql += "[" + column.ColumnName + "] " + "nvarchar(" + column.ToString.Length.ToString + ")" + "," ' <<< not very good
            Next
            sql = sql.TrimEnd(New Char() {","c}) + ")"
            Dim cmd As New OleDb.OleDbCommand(sql, con)
            Dim da As New OleDbDataAdapter(cmd)
            cmd.ExecuteNonQuery()
            Using adapter = New OleDbDataAdapter("SELECT * FROM tab1", con)
                Using builder = New OleDbCommandBuilder(adapter)
                    adapter.InsertCommand = builder.GetInsertCommand()
                    adapter.Update(dstable)
                End Using
            End Using
            con.Close()
            MessageBox.Show("Table Created Successfully")
        End Sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,159

    Re: From XML database to ACCESS database

    Moved from the VB.Net forum to the Code it Better forum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.