dcsimg
Results 1 to 3 of 3
  1. #1

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    93

    How do I export a datatable to a .dbf file?

    I have a datatable that I want to export to a .dbf file type. Everything I've found on the internet has not been helpful at all

  2. #2

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    93

    Re: How do I export a datatable to a .dbf file?

    Figured it out, this is my code:

    Code:
    Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;” & “Data Source=” & path & “;” & “Extended Properties=dBase IV”)
    
                dBaseConnection.Open()
    
                'New table
                Dim SQLCreateCommand As String
                'SQLCreateCommand = “CREATE TABLE NewTable ” & “(subdir TEXT(50))”
                Dim NewString As String = dt.ToString
                SQLCreateCommand = "SELECT * INTO NewTable From " & TableName
                Dim dBaseCommand As New System.Data.OleDb.OleDbCommand(SQLCreateCommand, dBaseConnection)
    
                dBaseCommand.ExecuteNonQuery()
                dBaseConnection.Close()

  3. #3

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    93

    Re: How do I export a datatable to a .dbf file?

    Several years later, I have found a better way of doing this using the FastDBF lib reference found here: https://github.com/SocialExplorer/FastDBF

    My previous solution required that a dbf with the same column structure already exits. The solution below creates a dbf with the columns that exist in the DataTable:

    Code:
    Private Sub ExportListButton_Click(sender As Object, e As EventArgs) Handles ExportListButton.Click
    
            Dim ExportedDbf = New DbfFile(Encoding.GetEncoding(1252))
            ExportedDbf.Open(Path.Combine("C:\WORK\A_TEST", "Combined.dbf"), FileMode.Create)
    
            For Each col In CombinedDataTable.Columns
                ExportedDbf.Header.AddColumn(New DbfColumn(col.ToString, DbfColumn.DbfColumnType.Character, 50, 0))
            Next
    
            Dim Counter As Integer = 0
    
            For Each row In CombinedDataTable.Rows
    
                Dim ColumnCounter As Integer = 0
                Dim NewRec = New DbfRecord(ExportedDbf.Header)
    
                For Each col In CombinedDataTable.Columns
    
                    NewRec(ColumnCounter) = CombinedDataTable.Rows(Counter)(col).ToString
                    ColumnCounter = ColumnCounter + 1
                Next
    
                ExportedDbf.Write(NewRec, True)
                Counter = Counter + 1
    
            Next
    
            ExportedDbf.Close()
    
        End Sub

Tags for this Thread

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