VB.NET将MSSQL数据写入DBF格式

时间:2017-04-22 09:22:08

标签: sql .net sql-server vb.net visual-studio

我正在尝试将MS-SQL数据导出到DBF format 无论如何要实现这一目标,因为我找不到任何解决办法吗? 我在 Visual Studio 2008 中使用 SQL Server 2008 Express R2 提前谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个链接:
https://www.pcreview.co.uk/threads/export-data-from-mssql-to-dbf-using-vb-net.1239926/

  

您好,

     

这并不像你想象的那么容易。我经常这样做,但我是第一个   必须创建我想要生成的.dbf表的结构然后我   将它放在指定子目录中的名称_中。我的原因   必须这样做是因为ado .net的odbc驱动程序是有限的   它将创建的数据类型,因此我不能很好地塑造.dbf文件   直接在.net。

     

下面的函数完成剩下的工作 - 它将给定的数据集转换为.dbf   文件,正如你想要的那样;我只是传递数据集,.dbf名称和   它的列数。

Public Function tabletodbf(ByVal mtable As DataSet, ByRef mdbf As String, ByVal numcols As Integer) As Integer
    ' suppositions: the dbf file is in f:\imcapps\dbffiles; also, there is
    ' an empty of it with a _ at the end of the filename; also, we are working
    ' with dbf files exclusively in f:\imcapps\dbffiles; also, the table and
    ' the dbf have to have the exact same structure and in the same column #
    ' sequence; also, if the _ causes
    ' the file name to be too large, this probably won't work, so I have to
    ' ensure this doesn't happen
    ' signature:
    ' dim funcs as new imcfunctionlib.functions
    ' dim xint as integer
    ' xint = funcs.tabletodbf(dspslips, "netcsv.dbf", 5)
    ' xint = funcs.tabletodbf(dsletsumtt2, "letsumtt.dbf", 27)
    tabletodbf = 0
    Dim oconn_ As New SqlConnection("data source=d5z0071;database=imc;integrated security=sspi;")
    Dim oconn_d_ As New OdbcConnection("Driver={Microsoft dBase Driver (*.dbf)};UID=admin;usercommitsync=yes;threads=3;statistics=0;safetransaction s=0;pagetimeout=5;maxscanrows=8;maxbuffersize=2048;FIL=dBaseIV;DriverID=533;deleted=0;defaultdir=f:\imcapps\dbffiles;dbq=f:\imcapps\dbffiles;collatingsequence=ascii;")
    oconn_.Open()
    oconn_d_.Open()
    Dim path As String = "f:\imcapps\dbffiles\" & mdbf
    Dim underscorename As String
    underscorename = Mid(mdbf, 1, mdbf.Length - 4) & "_.dbf"
    Dim fi As FileInfo = New FileInfo(path)
    If fi.Exists = True Then
        Kill("f:\imcapps\dbffiles\" & mdbf)
    End If
    FileCopy("f:\imcapps\dbffiles\" & underscorename, "f:\imcapps\dbffiles\" & mdbf)
    ' always save an empty file with _ as a convention
    Dim da_d As New OdbcDataAdapter("select * from f:\imcapps\dbffiles\" & mdbf, oconn_d_)
    Dim ds_d As New DataSet("_d")
    da_d.Fill(ds_d, "_d")
    Dim commandbuilder_ds_d As OdbcCommandBuilder = New OdbcCommandBuilder(da_d)
    Dim i As Integer
    Dim irow, mrow_d As DataRow
    For Each irow In mtable.Tables(0).Rows
        mrow_d = ds_d.Tables(0).NewRow()
        For i = 0 To numcols - 1
            mrow_d(i) = irow(i)
        Next
        ds_d.Tables("_d").Rows.Add(mrow_d)
    Next
    Try
        da_d.Update(ds_d, "_d")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    oconn_.Close()
    oconn_d_.Close()
    tabletodbf = 1
End Function

我不太确定......如果有效,你可以试试。