Domanda

I am trying to create a .DBF file using vb.net after figuring out that generating an access DB and afterwards converting it to .DBF was not really possible within vb.net

However, now I am encountering the following problem:

This is the code I am using:

Imports System.IO
Imports System.Data.SqlClient


Public Class frmStart
Inherits System.Windows.Forms.Form



Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

    Dim fd As FolderBrowserDialog = New FolderBrowserDialog
    Dim strFileName As String = ""
    Dim dbNameForm As New frmDatabaseName()

    ' init DB Vars
    Dim ds As New DataSet
    Dim sql As String


    ' Open Location browser
    dbNameForm.FileName = "Test"
    If dbNameForm.ShowDialog() = DialogResult.OK Then

        MsgBox("Select the location where you want your Access-File")

        If fd.ShowDialog() = DialogResult.OK Then
            Dim DatabaseFullPath As String = fd.SelectedPath & "\" & dbNameForm.FileName & ".DBF"

            'Create the Database
            CreateDatabase(DatabaseFullPath)
            MsgBox("Database created")

            'Open database
            Dim dBaseConnection As New System.Data.OleDb.OleDbConnection( _
                "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=" & DatabaseFullPath & ";" & _
                "Extended Properties=dBase IV")
            dBaseConnection.Open()
            ' Filling the Database with the required Columns

            sql = "CREATE TABLE DB_Total (NAME varchar(79),TYPE varchar(16), UNIT varchar(31)," &
                    "ADDR varchar(254), RAW_ZERO varchar(11), RAW_FULL varchar(11), ENG_ZERO varchar(11)," &
                    "ENG_FULL varchar(11), ENG_UNITS varchar(8), FORMAT varchar(11), COMMENT varchar(254)," &
                    "EDITCODE varchar(8), LINKED varchar(1), OID varchar(10), REF1 varchar(11), REF2 varchar(11)," &
                    "DEADBAND varchar(11), CUSTOM varchar(128), TAGGENLINK varchar(32), CLUSTER varchar(16)," &
                    "EQUIP varchar(254), ITEM varchar(63), HISTORIAN varchar(6)," &
                    "CUSTOM1 varchar(254), CUSTOM2 varchar(254), CUSTOM3 varchar(254), CUSTOM4 varchar(254)," &
                    "CUSTOM5 varchar(254), CUSTOM6 varchar(254), CUSTOM7 varchar(254), CUSTOM8 varchar(254))"
            Dim dBaseCommand As New System.Data.OleDb.OleDbCommand(sql, dBaseConnection)
            dBaseCommand.ExecuteNonQuery()
            dBaseCommand = Nothing

            sql = "CREATE TABLE TagSubs (TAGNAME varchar(79), POLLTIME varchar(6), SCALEMODE varchar(8), DEADBAND varchar(15))"
            dBaseCommand = New System.Data.OleDb.OleDbCommand(sql, dBaseConnection)
            dBaseCommand.ExecuteNonQuery()
            dBaseCommand = Nothing
            dBaseConnection.Close()

        Else

            MsgBox("Action Cancelled")

        End If

    Else

        MsgBox("Action Cancelled")

    End If

End Sub

     ' Creating a Database
Private Function CreateDatabase( _
    ByVal DatabaseFullPath As String) As Boolean
    Dim bAns As Boolean = False
    Dim cat As New ADOX.Catalog
    Try

        Dim sCreateString As String = _
          "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
          DatabaseFullPath
        cat.Create(sCreateString)

        bAns = True

    Catch Excep As System.Runtime.InteropServices.COMException

    Finally
        cat = Nothing
    End Try
    Return bAns
End Function

End Class

I am also Using a second Form, but that is just a windows in which a user can add a name to the file (Default, the name is "test.DBF" )

After I made this code, and I run it, I get the following error:

C:\Documents and settings\HIJ541\Desktop\Test.DBF is not a Valid path. 
Make sure the path name is spelled correctly and there is an active connection 
with the server on which the file is located 

(Roughly translated from Dutch)

Also, when I try to open the created .DBF file, it will tell me the following:

the file you are trying to open is in a different format than specified 
by the file extension

I think I am really in over my head on this one.. Any help would be greatly appreciated!

È stato utile?

Soluzione

I have solved my own question.

Apparently, when you are connecting to the .DBF file, you are not supposed to enter the full path name( Including the file name)

so:

Dim DatabaseFullPath As String = fd.SelectedPath & "\" & dbNameForm.FileName & ".DBF"

was supposed to be this:

Dim DatabaseFullPath As String = fd.SelectedPath & "\\"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top