Lire et générer un fichier dbase avec un code 100% .net

Soyez le premier à donner votre avis sur cette source.

Vue 5 404 fois - Téléchargée 708 fois

Description

Comment fabriquer un fichier dBase sans utiliser le driver MS Jet d'Access ? et sans utiliser Shapelib ? etc...
eh bien en utilisant mon code !

La classe DbfManager fournit les méthodes :
- ReadDBF
- WriteDBF2
- WriteDBF3Plus
- WriteDBF4
- WriteDBF5

D'une manière générale, seuls les premiers champs du header de fichier - ceux qui sont utiles à la gestion des données - sont gérés (structure CommonDBFHeader). Les autres champs sont très spécifiques.

La classe DBFManager est dans le fichier .zip.

Source / Exemple :


Exemple d'utilisation :

Imports System.Globalization
Imports dll_dbf_dotnet

Public Class Form1

    Private Const TARGET_DIR As String = "C:\Temp"
    Private Const TARGET_FILE_BASENAME As String = "dbf_file"

#Region "btnChargerDBFdotNet_Click"
    Private Sub btnChargerDBFdotNet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChargerDBFdotNet.Click

        Try
            Dim ofDlg As New OpenFileDialog
            If ofDlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim f As String = ofDlg.FileName

                Dim avant As DateTime = Now
                Dim contents As DBFContents = DBFManager.ReadDBF(f, (Me.chkAllStrings.CheckState = CheckState.Checked))
                Dim apres As DateTime = Now

                Dim diff As TimeSpan = apres - avant
                Console.WriteLine("Chargement du fichier " + f + " avec .NET en " + diff.Milliseconds.ToString + " milli secondes.")

                If contents.header.version = DBFManager.DBFVersion2 Then
                    Console.WriteLine("dBase II")
                    Console.WriteLine(contents.header.version)
                    Console.WriteLine(contents.header.updateYear)
                    Console.WriteLine(contents.header.updateMonth)
                    Console.WriteLine(contents.header.updateDay)
                    Console.WriteLine(contents.header.numRecords)
                    Console.WriteLine(contents.header.recordLength)

                    For Each fd As DBFFieldDescriptor In contents.fields
                        Console.Write(fd.name + " (" + fd.type + ") length=" _
                            + fd.length.ToString + vbCrLf)
                    Next
                    Console.Write(vbCrLf)

                    For j As Integer = 0 To contents.header.numRecords - 1
                        Dim fc As Integer = -1
                        For Each fd As DBFFieldDescriptor In contents.fields
                            fc += 1
                            If Me.chkAllStrings.CheckState = CheckState.Checked Then
                                Dim datas As List(Of String) = DirectCast(contents.data(fc), List(Of String))
                                Console.Write("<" + datas(j) + ">")
                            Else
                                Select Case fd.type
                                    Case DBFFieldType.DBFNumeric
                                        Dim datas As List(Of Decimal) = DirectCast(contents.data(fc), List(Of Decimal))
                                        Console.Write("<" + datas(j).ToString + ">")
                                    Case DBFFieldType.DBFCharacter
                                        Dim datas As List(Of String) = DirectCast(contents.data(fc), List(Of String))
                                        Console.Write("<" + datas(j) + ">")
                                    Case DBFFieldType.DBFLogical
                                        Dim datas As List(Of Boolean) = DirectCast(contents.data(fc), List(Of Boolean))
                                        Console.Write("<" + datas(j).ToString + ">")
                                    Case Else
                                        Console.Write("??? ")
                                End Select
                            End If
                        Next
                        Console.Write(vbCrLf)
                    Next

                End If

                If contents.header.version = DBFManager.dbfversion3 Then
                    Console.WriteLine("dBase III Plus/IV/V")
                    Console.WriteLine(contents.header.version)
                    Console.WriteLine(contents.header.updateYear)
                    Console.WriteLine(contents.header.updateMonth)
                    Console.WriteLine(contents.header.updateDay)
                    Console.WriteLine(contents.header.numRecords)
                    Console.WriteLine(contents.header.recordLength)

                    For Each fd As DBFFieldDescriptor In contents.fields
                        Console.Write(fd.name + " (" + fd.type + ") length=" _
                            + fd.length.ToString + vbCrLf)
                    Next
                    Console.Write(vbCrLf)

                    For j As Integer = 0 To contents.header.numRecords - 1
                        Dim fc As Integer = -1
                        For Each fd As DBFFieldDescriptor In contents.fields
                            fc += 1
                            If Me.chkAllStrings.CheckState = CheckState.Checked Then
                                Dim datas As List(Of String) = DirectCast(contents.data(fc), List(Of String))
                                Console.Write("<" + datas(j) + ">")
                            Else
                                Select Case fd.type
                                    Case DBFFieldType.DBFNumeric
                                        Dim datas As List(Of Decimal) = DirectCast(contents.data(fc), List(Of Decimal))
                                        Console.Write("<" + datas(j).ToString + ">")
                                    Case DBFFieldType.DBFCharacter
                                        Dim datas As List(Of String) = DirectCast(contents.data(fc), List(Of String))
                                        Console.Write("<" + datas(j) + ">")
                                    Case DBFFieldType.DBFDate
                                        Dim datas As List(Of DateTime) = DirectCast(contents.data(fc), List(Of DateTime))
                                        Console.Write("<" + datas(j).ToString + ">")
                                    Case DBFFieldType.DBFLogical
                                        Dim datas As List(Of Boolean) = DirectCast(contents.data(fc), List(Of Boolean))
                                        Console.Write("<" + datas(j).ToString + ">")
                                    Case DBFFieldType.DBFFloat
                                        Dim datas As List(Of Double) = DirectCast(contents.data(fc), List(Of Double))
                                        Console.Write("<" + datas(j).ToString + ">")
                                    Case Else
                                        Console.Write("??? ")
                                End Select
                            End If
                        Next
                        Console.Write(vbCrLf)
                    Next

                End If
            End If

        Catch ex_app As ApplicationException
            MessageBox.Show(ex_app.Message, "Erreur Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex_fnf As System.IO.FileNotFoundException
            MessageBox.Show(ex_fnf.Message, "Fichier introuvable", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
#End Region

#Region "btnWriteDBase3_Click"
    Private Sub btnWriteDBase3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteDBase3.Click
        Try
            Dim fields As New ArrayList
            fields.Add(DBFManager.CreateCharacterDBFField("ONE", 8))
            fields.Add(DBFManager.CreateLogicalDBFField("TWO"))
            fields.Add(DBFManager.CreateCharacterDBFField("THREE", 10))
            fields.Add(DBFManager.CreateNumericDBFField("the_fourth", 8, 4))
            fields.Add(DBFManager.CreateCharacterDBFField("FFFIVE", 5))
            fields.Add(DBFManager.CreateDateDBFField("my_date"))

            Dim datas As New List(Of Object)

            Dim los As New List(Of String)
            los.Add("abcd")
            los.Add("efghijk")
            los.Add("kk")
            los.Add("hh")
            los.Add("gg")
            los.Add("dd")
            los.Add("rty")
            los.Add("aze")
            datas.Add(los)

            Dim lob As New List(Of Boolean)
            lob.Add(True)
            lob.Add(False)
            lob.Add(False)
            lob.Add(False)
            lob.Add(False)
            lob.Add(True)
            lob.Add(False)
            lob.Add(True)
            datas.Add(lob)

            Dim los1 As New List(Of String)
            los1.Add("once")
            los1.Add("upon")
            los1.Add("a time!!!")
            los1.Add("the")
            los1.Add("world")
            los1.Add("is")
            los1.Add("not")
            los1.Add("enought")
            datas.Add(los1)

            Dim lod As New List(Of Decimal)
            lod.Add(321.45D)
            lod.Add(-0.0254D)
            lod.Add(1.01D)
            lod.Add(0.07D)
            lod.Add(7)
            lod.Add(0.007D)
            lod.Add(0.07D)
            lod.Add(1.01D)
            datas.Add(lod)

            Dim los2 As New List(Of String)
            los2.Add("i'm")
            los2.Add("the")
            los2.Add("BEST")
            los2.Add("BEST")
            los2.Add("BEST")
            los2.Add("BEST")
            los2.Add("BEST")
            los2.Add("BEST")
            datas.Add(los2)

            Dim lodat As New List(Of DateTime)
            lodat.Add(Date.Now)
            lodat.Add(New DateTime(1985, 12, 25))
            lodat.Add(New DateTime(2001, 9, 11))
            lodat.Add(New DateTime(1977, 8, 20))
            lodat.Add(New DateTime(2000, 11, 11))
            lodat.Add(New DateTime(2007, 1, 2))
            lodat.Add(New DateTime(1945, 12, 24))
            lodat.Add(New DateTime(1921, 4, 22))
            datas.Add(lodat)

            ' 8 records added
            If (Me.chkCompleteHeaders.CheckState = CheckState.Checked) OrElse _
                (Me.chkCompleteHeaders.CheckState = CheckState.Unchecked) Then
                DBFManager.WriteDBF3Plus(TARGET_DIR + "\" + TARGET_FILE_BASENAME + "3.dbf", _
                    fields, 8, datas)
            End If

        Catch ex_app As ApplicationException
            MessageBox.Show(ex_app.Message, "Erreur Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
#End Region

#Region "btnWriteDBase4_Click"
    Private Sub btnWriteDBase4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteDBase4.Click
        Try
            Dim fields As New ArrayList
            fields.Add(DBFManager.CreateCharacterDBFField("ONE", 16))
            fields.Add(DBFManager.CreateLogicalDBFField("TWO"))
            fields.Add(DBFManager.CreateCharacterDBFField("THREE", 30))
            fields.Add(DBFManager.CreateNumericDBFField("the_fourth", 8, 4))
            fields.Add(DBFManager.CreateCharacterDBFField("FFFIVE", 5))
            fields.Add(DBFManager.CreateFloatDBFField("Double1", 7, 3))
            fields.Add(DBFManager.CreateDateDBFField("my_date"))
            fields.Add(DBFManager.CreateFloatDBFField("double2", 10, 3))

            Dim datas As New List(Of Object)

            Dim los As New List(Of String)
            ' first record
            los.Add("abcd")
            ' second record
            los.Add("efghijk")
            ' third record
            los.Add("kk")
            datas.Add(los)

            Dim lob As New List(Of Boolean)
            ' first record
            lob.Add(True)
            ' second record
            lob.Add(False)
            ' third record
            lob.Add(False)
            datas.Add(lob)

            Dim los1 As New List(Of String)
            ' first record
            los1.Add("once")
            ' second record
            los1.Add("opon")
            ' third record
            los1.Add("a timeeeeeeeeeeeeeeeeejj")
            datas.Add(los1)

            Dim lod As New List(Of Decimal)
            ' first record
            lod.Add(321.45D)
            ' second record
            lod.Add(-0.0254D)
            ' third record
            lod.Add(1.01D)
            datas.Add(lod)

            Dim los2 As New List(Of String)
            ' first record
            los2.Add("i'm")
            ' second record
            los2.Add("the")
            ' third record
            los2.Add("BEST")
            datas.Add(los2)

            Dim lodbl1 As New List(Of Double)
            ' first record
            lodbl1.Add(-25.63)
            ' second record
            lodbl1.Add(666.6)
            ' third record
            lodbl1.Add(0.666)
            datas.Add(lodbl1)

            Dim lodat As New List(Of DateTime)
            ' first record
            lodat.Add(Date.Now)
            ' second record
            lodat.Add(New DateTime(1985, 12, 25))
            ' third record
            lodat.Add(New DateTime(2001, 9, 11))
            datas.Add(lodat)

            Dim lodbl2 As New List(Of Double)
            ' first record
            lodbl2.Add(-5.055)
            ' second record
            lodbl2.Add(11)
            ' third record
            lodbl2.Add(999999)
            datas.Add(lodbl2)

            ' 3 records added

            If Me.chkCompleteHeaders.CheckState = CheckState.Checked Then
                DBFManager.WriteDBF4(TARGET_DIR + "\" + TARGET_FILE_BASENAME + "4.dbf", _
                    fields, 3, datas)
            Else
                ' four extra flags are set to 0 because I don't know exactly what to specify
                DBFManager.WriteDBF4(TARGET_DIR + "\" + TARGET_FILE_BASENAME + "4.dbf", _
                    fields, 3, datas, 0, 0, 0, 0)
            End If

        Catch ex_app As ApplicationException
            MessageBox.Show(ex_app.Message, "Erreur Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
#End Region

#Region "btnWriteDBase5_Click"
    Private Sub btnWriteDBase5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteDBase5.Click
        Try
            Dim fields As New ArrayList
            fields.Add(DBFManager.CreateCharacterDBFField("ONE", 16))
            fields.Add(DBFManager.CreateLogicalDBFField("TWO"))
            fields.Add(DBFManager.CreateCharacterDBFField("THREE", 30))
            fields.Add(DBFManager.CreateNumericDBFField("the_fourth", 8, 4))
            fields.Add(DBFManager.CreateCharacterDBFField("FFFIVE", 5))
            fields.Add(DBFManager.CreateFloatDBFField("Double1", 7, 3))
            fields.Add(DBFManager.CreateDateDBFField("my_date"))
            fields.Add(DBFManager.CreateFloatDBFField("double2", 10, 3))

            Dim datas As New List(Of Object)

            Dim los As New List(Of String)
            ' first record
            los.Add("abcd")
            ' second record
            los.Add("efghijk")
            ' third record
            los.Add("kk")
            datas.Add(los)

            Dim lob As New List(Of Boolean)
            ' first record
            lob.Add(True)
            ' second record
            lob.Add(False)
            ' third record
            lob.Add(False)
            datas.Add(lob)

            Dim los1 As New List(Of String)
            ' first record
            los1.Add("once")
            ' second record
            los1.Add("upon")
            ' third record
            los1.Add("a time")
            datas.Add(los1)

            Dim lod As New List(Of Decimal)
            ' first record
            lod.Add(321.45D)
            ' second record
            lod.Add(-0.0254D)
            ' third record
            lod.Add(1.01D)
            datas.Add(lod)

            Dim los2 As New List(Of String)
            ' first record
            los2.Add("i'm")
            ' second record
            los2.Add("the")
            ' third record
            los2.Add("BEST")
            datas.Add(los2)

            Dim lodbl1 As New List(Of Double)
            ' first record
            lodbl1.Add(-25.63)
            ' second record
            lodbl1.Add(666.6)
            ' third record
            lodbl1.Add(0.666)
            datas.Add(lodbl1)

            Dim lodat As New List(Of DateTime)
            ' first record
            lodat.Add(Date.Now)
            ' second record
            lodat.Add(New DateTime(1985, 12, 25))
            ' third record
            lodat.Add(New DateTime(2001, 9, 11))
            datas.Add(lodat)

            Dim lodbl2 As New List(Of Double)
            ' first record
            lodbl2.Add(-5.055)
            ' second record
            lodbl2.Add(11)
            ' third record
            lodbl2.Add(999999)
            datas.Add(lodbl2)

            ' 3 records added

            If Me.chkCompleteHeaders.CheckState = CheckState.Checked Then
                DBFManager.WriteDBF5(TARGET_DIR + "\" + TARGET_FILE_BASENAME + "5.dbf", _
                    fields, 3, datas)
            Else
                ' four extra flags are set to 0 because I don't know exactly what to specify
                DBFManager.WriteDBF5(TARGET_DIR + "\" + TARGET_FILE_BASENAME + "5.dbf", _
                    fields, 3, datas, 0, 0, 0, 0)
            End If

        Catch ex_app As ApplicationException
            MessageBox.Show(ex_app.Message, "Erreur Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
#End Region

#Region "btnWriteDbase2_Click"
    Private Sub btnWriteDbase2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnWriteDbase2.Click

        Try
            Dim fields As New ArrayList
            fields.Add(DBFManager.CreateCharacterDBF2Field("ONE", 8))
            fields.Add(DBFManager.CreateLogicalDBF2Field("TWO"))
            fields.Add(DBFManager.CreateNumericDBF2Field("the_fourth", 8, 4))

            Dim datas As New List(Of Object)

            Dim los As New List(Of String)
            los.Add("abcd")
            los.Add("efghijk")
            los.Add("kk")
            los.Add("hh")
            los.Add("gg")
            los.Add("dd")
            los.Add("rty")
            los.Add("aze")
            datas.Add(los)

            Dim lob As New List(Of Boolean)
            lob.Add(True)
            lob.Add(False)
            lob.Add(False)
            lob.Add(False)
            lob.Add(False)
            lob.Add(True)
            lob.Add(False)
            lob.Add(True)
            datas.Add(lob)

            Dim lod As New List(Of Decimal)
            lod.Add(321.45D)
            lod.Add(-0.0254D)
            lod.Add(1.01D)
            lod.Add(0.07D)
            lod.Add(7)
            lod.Add(0.007D)
            lod.Add(0.07D)
            lod.Add(1.01D)
            datas.Add(lod)

            ' 8 records added

            DBFManager.WriteDBF2(TARGET_DIR + "\" + TARGET_FILE_BASENAME + "2.dbf", _
                fields, 8, datas)

        Catch ex_app As ApplicationException
            MessageBox.Show(ex_app.Message, "Erreur Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
#End Region

End Class

Conclusion :


je vous laisse faire ...

Codes Sources

A voir également

Ajouter un commentaire

Commentaires

papijak
Messages postés
1
Date d'inscription
vendredi 20 décembre 2002
Statut
Membre
Dernière intervention
29 septembre 2014
-
Où trouver dll_dbf_dotnet qu'il faut importer sinon la source ne vaut rien
jordane45
Messages postés
26811
Date d'inscription
mercredi 22 octobre 2003
Statut
Modérateur
Dernière intervention
21 novembre 2019
317 > papijak
Messages postés
1
Date d'inscription
vendredi 20 décembre 2002
Statut
Membre
Dernière intervention
29 septembre 2014
-
Bonjour,
La source date de 2012... elle semble avoir été téléchargée plus de 300 fois....
Si quelqu'un avait rencontré un souci je pense qu'un commentaire aurait été mis avant le tient....
Je me demande si le fichier dll_dbf ne serait pas en fait, DbfManager... (joint dans le Zip de la source).

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.