Answered Creating DBF IN C# using OLEDB

  • Wednesday, January 17, 2007 2:30 PM
     
     
    is it possible?

    can any one show me the way of doing it?

    thanks for you help.

Answers

  • Wednesday, January 17, 2007 4:52 PM
     
     Answered
    Managed oledb (and other managed ado.net providers) doesn't offer such feature. But you can issue DDL sql commands to build a database if driver supports this.
  • Wednesday, January 17, 2007 9:40 PM
     
     Answered

    Hi Irene,

    A lot depends on what kind of DBF you want to create. There are several ODBC drivers and OLE DB data providers you can use to create the DBFs. You can use Jet, the latest FoxPro and Visual FoxPro ODBC driver, downloadable from msdn.microsoft.com/vfoxpro/downloads/updates/odbc, or use The latest FoxPro and Visual FoxPro OLE DB data provider,
    downloadable from msdn.microsoft.com/vfoxpro/downloads/updates.

    Jet will create DBFs in the older Dbase IV format. Over the years, new data features have been added to the Fox DBF structure. These new features are not compatible with Jet and will not open natively in Excel, for example. The Fox ODBC and OLE DB will create VFP-compatible DBFs in the newer format.

    Here is some VB.NET code that will create a DBF and perform some CRUD operations. You can paste it into a VB.NET console application project. I'll let you work out the C# code. You can read more about the SQL commands the VFP OLE DB data provider supports in the VFP Help in the MSDN Library.

     

    Imports System
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Data.SqlClient

    Module Module1

        Sub Main()

            Try

                Dim cn1 As New OleDbConnection( _
                    "Provider=VFPOLEDB.1;Data Source=C:\Temp\;")
                cn1.Open()
                '-- Make some VFP data to play with
                Dim cmd1 As New OleDbCommand( _
                    "Create Table TestDBF (Field1 I, Field2 C(10))", cn1)
                Dim cmd2 As New OleDbCommand( _
                    "Insert Into TestDBF Values (1, 'Hello')", cn1)
                Dim cmd3 As New OleDbCommand( _
                    "Insert Into TestDBF Values (2, 'World')", cn1)
                cmd1.ExecuteNonQuery()
                cmd2.ExecuteNonQuery()
                cmd3.ExecuteNonQuery()
                cn1.Close()

                Dim cn2 As New OleDbConnection( _
                    "Provider=VFPOLEDB.1;Data Source=C:\Temp\;")
                cn2.Open()

                Dim cmd4 As New OleDbCommand( _
                    "Select * From TestDBF", cn2)
                Dim da1 As New OleDbDataAdapter(cmd4)
                Dim ds1 As New DataSet
                Dim dr1 As DataRow
                da1.Fill(ds1)
                For Each dr1 In ds1.Tables(0).Rows
                    Console.WriteLine(dr1.Item(1).ToString())
                Next
                Console.ReadLine()
                cn2.Close()

            Catch e As Exception
                MsgBox(e.ToString())
            End Try

        End Sub

    End Module



     

  • Thursday, January 18, 2007 1:23 PM
     
     Answered


    You can also use Jet OLEDB:

    Dim dBaseConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _

    "Data Source=" & "e:\My Documents\dBase" & ";" & _

    "Extended Properties=dBase IV")

    dBaseConnection.Open()

    Dim SQLCreateCommand As String

    SQLCreateCommand = "CREATE TABLE Customer " & _

    "(CustomerID INTEGER," & _

    "LastName TEXT(50), " & _

    "FirstName TEXT(50)," & _

    "Phone TEXT(10)," & _

    "Email TEXT(50))"

    Dim dBaseCommand As New System.Data.OleDb.OleDbCommand(SQLCreateCommand, dBaseConnection)

    dBaseCommand.ExecuteNonQuery()

    dBaseConnection.Close()

All Replies

  • Wednesday, January 17, 2007 4:52 PM
     
     Answered
    Managed oledb (and other managed ado.net providers) doesn't offer such feature. But you can issue DDL sql commands to build a database if driver supports this.
  • Wednesday, January 17, 2007 9:40 PM
     
     Answered

    Hi Irene,

    A lot depends on what kind of DBF you want to create. There are several ODBC drivers and OLE DB data providers you can use to create the DBFs. You can use Jet, the latest FoxPro and Visual FoxPro ODBC driver, downloadable from msdn.microsoft.com/vfoxpro/downloads/updates/odbc, or use The latest FoxPro and Visual FoxPro OLE DB data provider,
    downloadable from msdn.microsoft.com/vfoxpro/downloads/updates.

    Jet will create DBFs in the older Dbase IV format. Over the years, new data features have been added to the Fox DBF structure. These new features are not compatible with Jet and will not open natively in Excel, for example. The Fox ODBC and OLE DB will create VFP-compatible DBFs in the newer format.

    Here is some VB.NET code that will create a DBF and perform some CRUD operations. You can paste it into a VB.NET console application project. I'll let you work out the C# code. You can read more about the SQL commands the VFP OLE DB data provider supports in the VFP Help in the MSDN Library.

     

    Imports System
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Data.SqlClient

    Module Module1

        Sub Main()

            Try

                Dim cn1 As New OleDbConnection( _
                    "Provider=VFPOLEDB.1;Data Source=C:\Temp\;")
                cn1.Open()
                '-- Make some VFP data to play with
                Dim cmd1 As New OleDbCommand( _
                    "Create Table TestDBF (Field1 I, Field2 C(10))", cn1)
                Dim cmd2 As New OleDbCommand( _
                    "Insert Into TestDBF Values (1, 'Hello')", cn1)
                Dim cmd3 As New OleDbCommand( _
                    "Insert Into TestDBF Values (2, 'World')", cn1)
                cmd1.ExecuteNonQuery()
                cmd2.ExecuteNonQuery()
                cmd3.ExecuteNonQuery()
                cn1.Close()

                Dim cn2 As New OleDbConnection( _
                    "Provider=VFPOLEDB.1;Data Source=C:\Temp\;")
                cn2.Open()

                Dim cmd4 As New OleDbCommand( _
                    "Select * From TestDBF", cn2)
                Dim da1 As New OleDbDataAdapter(cmd4)
                Dim ds1 As New DataSet
                Dim dr1 As DataRow
                da1.Fill(ds1)
                For Each dr1 In ds1.Tables(0).Rows
                    Console.WriteLine(dr1.Item(1).ToString())
                Next
                Console.ReadLine()
                cn2.Close()

            Catch e As Exception
                MsgBox(e.ToString())
            End Try

        End Sub

    End Module



     

  • Wednesday, January 17, 2007 11:51 PM
     
     
    it's works,
    thanks for the help.
  • Thursday, January 18, 2007 3:33 AM
     
     
    can anyone show me the way of creating new dbase Version 4 without using the fox pro oledb?
  • Thursday, January 18, 2007 1:23 PM
     
     Answered


    You can also use Jet OLEDB:

    Dim dBaseConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _

    "Data Source=" & "e:\My Documents\dBase" & ";" & _

    "Extended Properties=dBase IV")

    dBaseConnection.Open()

    Dim SQLCreateCommand As String

    SQLCreateCommand = "CREATE TABLE Customer " & _

    "(CustomerID INTEGER," & _

    "LastName TEXT(50), " & _

    "FirstName TEXT(50)," & _

    "Phone TEXT(10)," & _

    "Email TEXT(50))"

    Dim dBaseCommand As New System.Data.OleDb.OleDbCommand(SQLCreateCommand, dBaseConnection)

    dBaseCommand.ExecuteNonQuery()

    dBaseConnection.Close()