2023年6月27日 星期二

Acessando arquivos DBF com C# e VB .NET

 

.NET - Acessando arquivos DBF com C# e VB .NET

--


Declare os seguintes namespaces no formulário:


Imports System.Data.Odbc

Imports System.Data


No evento Click do botão inclua o código abaixo:


 Private Sub btnDBF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDBF.Click


        Dim oConn As New OdbcConnection()

        oConn.ConnectionString = "Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=c:\dados\;Exclusive=No; _

                                                Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;"

        oConn.Open()

        Dim oCmd As OdbcCommand = oConn.CreateCommand()

        oCmd.CommandText = "SELECT * FROM c:\dados\arqDBF.dbf"

        Dim dt As New DataTable()

        dt.Load(oCmd.ExecuteReader())

        oConn.Close()


        DataGridView1.DataSource = dt


    End Sub




Podemos obter o mesmo resultado usando provedor OleDb , neste caso usamos o provedor OleDb:


Imports System.Data.OleDb

Imports System.Data


Substitua o código do evento Click do botão de comando pelo código abaixo:


Dim oConn As New OleDbConnection()

oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\dados;Extended Properties=dBASE IV;"


oConn.Open()

Dim oCmd As OleDbCommand = oConn.CreateCommand()

oCmd.CommandText = "SELECT * FROM c:\dados\arqDBF.dbf"

Dim dt As New DataTable()

dt.Load(oCmd.ExecuteReader())

oConn.Close()

dataGridView1.DataSource = dt


VBNET InsertRow DBF

 Private Sub InsertRow(ByVal Field1 As String, ByVal Field2 As String, ByVal Field3 As String)

        'see above 

        'here you would set what Typ of Data

        ' I just used Field1 to 3 as a String


        Dim con As OleDbConnection

        Dim dBaseFile As String


        dBaseFile = "TestData.dbf"

        Try

            con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;Extended Properties=dBASE IV;")


            'Create a new instance of the command object

            Using cmd As OleDbCommand = New OleDbCommand("INSERT INTO " & dBaseFile & " ([Field1], [Field2], [Field3]) VALUES (@f1, @f2, @f3)", con)

                'Parameterize the query

                With cmd.Parameters

                    .AddWithValue("@f1", Field1)

                    .AddWithValue("@f2", Field2)

                    .AddWithValue("@f3", Field3)

                End With

                con.Open()

                cmd.ExecuteNonQuery()

                con.Close()

            End Using

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        Finally

            If con IsNot Nothing Then

                If con.State = ConnectionState.Open Then

                    con.Close()

                End If

                con.Dispose()

            End If

        End Try

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'fill Textboxes and ADD Datarow

        InsertRow(Field1:=TextBox1.Text, Field2:=TextBox2.Text, Field3:=TextBox3.Text)

    End Sub


Using .dbf database in vb.net

 Imports System.Data.OleDb


Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strSql As String

        strSql = "Select * From TestData.dbf"

        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;Extended Properties=dBASE IV;")

        Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)

        con.Open()

        Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

        Dim myDataSet As DataSet = New DataSet()

        myDA.Fill(myDataSet, "MyT")

        Dgv.DataSource = myDataSet.Tables("MyT").DefaultView

        con.Close()

        con = Nothing

    End Sub

End Class

DATAGRIDVIEW載入DBF檔 (VB.NET)

 DBF檔請先匯出成dbase IV 格式



Dim cnn As OleDb.OleDbConnection

cnn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\POLLYPRO\DATA\NSAMPLE;Extended Properties=DBASE IV;")

Dim da As New OleDbDataAdapter("Select * From test.DBF", cnn)

Dim ds As New DataSet

da.Fill(ds)

GridView1.DataSource = ds.Tables(0)


VB.Net 使用 dBase / FoxPro 資料庫

 VB.Net 如何存取 xBase / FoxPro 資料庫


<< 使用OleDb >>


Imports System.Data.OleDb


Dim strCn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _


"Data Source=C:\DBF檔案所在資料夾;" & _


"Extended Properties=dBASE IV;" & _


"User ID=Admin;Password=;"


Dim strQry As String = "Select * From 某某.dbf"


Dim cn As New OleDbConnection(strCn)


Dim da As New OleDbDataAdapter(strQry, cn)


Dim ds As New DataSet


da.Fill(ds)


DataGridView1.DataSource = ds.Tables(0)


PS : ConnectionString 的 Extended Properties 可為 dBASE IV 或 dBASE 5.0 或 dBASE III。


也可以是 FoxPro 2.0 、 FoxPro 2.5 、 FoxPro 2.6 、 FoxPro 3.0


' ================================================================


<< 使用ODBC >>


Imports System.Data.Odbc


Dim strCn As String = "DRIVER={Microsoft dBase Driver (*.dbf)};dbq=C:\DBF檔案所在資料夾"


Dim strQry As String = "Select * From 某某.DBF"


Dim cn As New OdbcConnection(strCn)


Dim da As New OdbcDataAdapter(strQry, cn)


Dim ds As New DataSet


da.Fill(ds)


DataGridView1.DataSource = ds.Tables(0)


PS : ConnectionString 的 Driver 可為 [ODBC 資料來源管理員] 中所提供的 驅動程式。


如:


"DRIVER={Microsoft dBase Driver (*.dbf)};dbq=C:\DBF檔案所在資料夾"


"DRIVER={Microsoft dBase VFP Driver (*.dbf)};dbq=C:\DBF檔案所在資料夾"


"DRIVER={Microsoft FoxPro Driver (*.dbf)};dbq=C:\DBF檔案所在資料夾"


"DRIVER={Microsoft FoxPro VFP Driver (*.dbf)};dbq=C:\DBF檔案所在資料夾"


"DRIVER={Microsoft Visual FoxPro Driver (*.dbf)};dbq=C:\DBF檔案所在資料夾"


xBase 或 FoxPro 資料庫,只要有 ODBC Driver,皆可使用相同方式來連線存取。


2023年6月15日 星期四

檢查 form 是否已開

 For Each frm As Form In Application.OpenForms

    If frm.Name = Form1.Name Then

       MessageBox.Show("Opened")

    End If

Next