使用 vb.net 生成新的 DBF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16140180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:20:58  来源:igfitidea点击:

Generating a new DBF file using vb.net

databasevb.netdatabase-designdbf

提问by Gutanoth

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

在弄清楚生成访问数据库并随后将其转换为 .DBF 在 vb.net 中是不可能的之后,我试图使用 vb.net 创建一个 .DBF 文件

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" )

我也在使用第二个表单,但这只是一个窗口,用户可以在其中向文件添加名称(默认,名称为 "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:

此外,当我尝试打开创建的 .DBF 文件时,它会告诉我以下内容:

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!

我想我真的在这个问题上不知所措.. 任何帮助将不胜感激!

回答by Gutanoth

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)

显然,当您连接到 .DBF 文件时,您不应该输入完整路径名(包括文件名)

so:

所以:

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

was supposed to be this:

应该是这样的:

Dim DatabaseFullPath As String = fd.SelectedPath & "\"