none
How to use .dbf file to vb.net RRS feed

  • Question

  • Good Day,

    I would like to ask question.  How can i use .dbf file in vb.net?

    Thank you.

    Thursday, April 25, 2013 9:40 AM

Answers

  • U have to use ODBC classes (a series of things to that) in VB.NET.

    But until now, FoxPro seems not popular, and I suggest u using SQL instead.


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    Thursday, April 25, 2013 9:42 AM
  • Hello,

    Here is an example of reading data from a DBF in the app folder.

    Imports System.Data.OleDb
    Public Class Form1
        Private FileName As String = IO.Path.Combine(Application.StartupPath, "Customer.dbf")
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If IO.File.Exists(FileName) Then
                Dim Builder As New OleDbConnectionStringBuilder With
                    {
                        .DataSource = IO.Path.GetDirectoryName(FileName),
                        .Provider = "Microsoft.Jet.OLEDB.4.0"
                    }
                Builder.Add("Extended Properties", "dBase III")
                Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
                    Using cmd As New OleDbCommand With {.Connection = cn}
                        cmd.CommandText = "SELECT * FROM " & IO.Path.GetFileName(FileName)
                        cn.Open()
                        Dim dt As New DataTable
                        dt.Load(cmd.ExecuteReader)
                    End Using
                End Using
            End If
        End Sub
    End Class

    Inserting a new record

    Imports System.Data.OleDb
    Public Class Form1
        Private FileName As String = IO.Path.Combine(Application.StartupPath, "Customer.dbf")
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If IO.File.Exists(FileName) Then
                Dim Builder As New OleDbConnectionStringBuilder With
                    {
                        .DataSource = IO.Path.GetDirectoryName(FileName),
                        .Provider = "Microsoft.Jet.OLEDB.4.0"
                    }
                Builder.Add("Extended Properties", "dBase III")
                Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
                    Using cmd As New OleDbCommand With {.Connection = cn}
                        cmd.CommandText = _
                        <SQL>
                            INSERT INTO 123456789 (FIELD1, FIELD2) 
                            VALUES ('Hello','World')
                        </SQL>.Value
                        cn.Open()
                        Dim Affected = cmd.ExecuteNonQuery()
                        If Affected = 1 Then
                            MessageBox.Show("Inserted row")
                        Else
                            MessageBox.Show("Insert failed")
                        End If
                    End Using
                End Using
            End If
        End Sub
    End Class

    Also consider that these code has not opened the index file is there is one thus if another app uses the DBF and opens the index it will be incorrect at this point.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, April 25, 2013 11:40 AM
    Moderator

All replies

  • U have to use ODBC classes (a series of things to that) in VB.NET.

    But until now, FoxPro seems not popular, and I suggest u using SQL instead.


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    Thursday, April 25, 2013 9:42 AM
  • Hello,

    Here is an example of reading data from a DBF in the app folder.

    Imports System.Data.OleDb
    Public Class Form1
        Private FileName As String = IO.Path.Combine(Application.StartupPath, "Customer.dbf")
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If IO.File.Exists(FileName) Then
                Dim Builder As New OleDbConnectionStringBuilder With
                    {
                        .DataSource = IO.Path.GetDirectoryName(FileName),
                        .Provider = "Microsoft.Jet.OLEDB.4.0"
                    }
                Builder.Add("Extended Properties", "dBase III")
                Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
                    Using cmd As New OleDbCommand With {.Connection = cn}
                        cmd.CommandText = "SELECT * FROM " & IO.Path.GetFileName(FileName)
                        cn.Open()
                        Dim dt As New DataTable
                        dt.Load(cmd.ExecuteReader)
                    End Using
                End Using
            End If
        End Sub
    End Class

    Inserting a new record

    Imports System.Data.OleDb
    Public Class Form1
        Private FileName As String = IO.Path.Combine(Application.StartupPath, "Customer.dbf")
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If IO.File.Exists(FileName) Then
                Dim Builder As New OleDbConnectionStringBuilder With
                    {
                        .DataSource = IO.Path.GetDirectoryName(FileName),
                        .Provider = "Microsoft.Jet.OLEDB.4.0"
                    }
                Builder.Add("Extended Properties", "dBase III")
                Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
                    Using cmd As New OleDbCommand With {.Connection = cn}
                        cmd.CommandText = _
                        <SQL>
                            INSERT INTO 123456789 (FIELD1, FIELD2) 
                            VALUES ('Hello','World')
                        </SQL>.Value
                        cn.Open()
                        Dim Affected = cmd.ExecuteNonQuery()
                        If Affected = 1 Then
                            MessageBox.Show("Inserted row")
                        Else
                            MessageBox.Show("Insert failed")
                        End If
                    End Using
                End Using
            End If
        End Sub
    End Class

    Also consider that these code has not opened the index file is there is one thus if another app uses the DBF and opens the index it will be incorrect at this point.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, April 25, 2013 11:40 AM
    Moderator
  • I just want to know, if this works on 2.0 framework of vb.net? :D

    Geee

    Thursday, October 23, 2014 8:55 AM
  • The answer is yes, but really you should be asking a new question since this one is well over a year old and very few people will see your inquiry.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, October 24, 2014 12:28 PM