1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| Imports ADODB
Imports ADOX
Public Class BaseAccess
Private mNomBase As String
Private mCat As ADOX.Catalog
Public ReadOnly Property Cat() As ADOX.Catalog
Get
Return mCat
End Get
End Property
Public Property NomBase() As String
Get
Return mNomBase
End Get
Set(ByVal value As String)
mNomBase = value
End Set
End Property
Private mChemin As String
Public Property Chemin() As String
Get
Return mChemin
End Get
Set(ByVal value As String)
If Mid$(value, Len(value), 1) <> "\" Then
value = value & "\"
End If
mChemin = value
End Set
End Property
'existence de la base de donnée
Public Function Exist() As Boolean
Dim ex As Boolean = My.Computer.FileSystem.FileExists(mChemin & mNomBase & ".accdb")
Return ex
End Function
'ouverture de la base
Public Sub New(ByVal Nom As String, ByVal chemin As String)
' affectation chemin et nombase
mNomBase = Nom
Me.Chemin = chemin
' creation de l'objet permanent catalogue
mCat = New ADOX.Catalog
' si la base n'existe pas création
If Not Exist() Then
On Error Resume Next
mCat = mCat.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mChemin & mNomBase & ".accdb;")
On Error GoTo 0
End If
'affectation de la connexion à la base pour l'objet catalogue
Dim cnn As New ADODB.Connection
cnn.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mChemin & mNomBase & ".accdb;")
mCat.ActiveConnection = cnn
End Sub
Public Sub CreationTableID(ByVal nomtable As String)
CreationTable(nomtable, mCat)
End Sub
Public Sub CreationTableJointure(ByVal nomtable As String, ByVal nomtableprimaire As String)
CreationTable(nomtable, mCat, nomtableprimaire)
End Sub
Public Sub CreationTableJointurenn(ByVal nomtable1 As String, ByVal nomtable2 As String)
CreationTable(nomtable1, nomtable2, mCat)
End Sub
' creation table de base affectation auto de la clef primaire ID_nomdelatable
Private Sub CreationTable(ByVal nom As String, ByVal cat As ADOX.Catalog)
Dim table As New ADOX.Table
table.Name = nom
Dim champs As New ADOX.Column
champs.Name = "ID_" & nom
champs.Type = ADOX.DataTypeEnum.adInteger
champs.ParentCatalog = cat
champs.Properties("Autoincrement").Value = True
champs.Properties("Seed").Attributes = 1
champs.Properties("Increment").Attributes = 1
table.Columns.Append(champs)
Dim clef As New ADOX.Key
clef.Type = KeyTypeEnum.adKeyPrimary
clef.Name = "clefprim"
clef.Columns.Append("ID_" & nom)
table.Keys.Append(clef)
cat.Tables.Append(table)
End Sub
'surcharge création d'une table liée à la table ptrname
'la table ptrname doit existante ptrname est la table coté 1 de la relation
Private Sub CreationTable(ByVal nom As String, ByVal cat As ADOX.Catalog, ByVal PtrName As String)
Dim table As New ADOX.Table
table.Name = nom
Dim champs As New ADOX.Column
champs.Name = "ID_" & nom
champs.Type = ADOX.DataTypeEnum.adInteger
champs.ParentCatalog = cat
champs.Properties("Autoincrement").Value = True
champs.Properties("Seed").Attributes = 1
champs.Properties("Increment").Attributes = 1
table.Columns.Append(champs)
Dim clef As New ADOX.Key
clef.Type = KeyTypeEnum.adKeyPrimary
clef.Name = "clefprim"
clef.Columns.Append("ID_" & nom)
table.Keys.Append(clef)
champs = New ADOX.Column
champs.Name = "Ptr_" & PtrName
champs.Type = ADOX.DataTypeEnum.adInteger
table.Columns.Append(champs)
'champs.ParentCatalog = cat
clef = New ADOX.Key
clef.Type = KeyTypeEnum.adKeyForeign
clef.Name = "clef_" & PtrName & nom
clef.RelatedTable = PtrName
clef.Columns.Append("Ptr_" & PtrName)
clef.Columns("Ptr_" & PtrName).RelatedColumn = "ID_" & PtrName
table.Keys.Append(clef)
cat.Tables.Append(table)
End Sub
'surcharge création d'une table de jointure pour liaison n-n
'fournir le nom des deux tables de chaque coté de la relation
Private Sub CreationTable(ByVal nom1 As String, ByVal nom2 As String, ByVal cat As ADOX.Catalog)
Dim table As New ADOX.Table
table.Name = "Joint" & nom1 & nom2
Dim champs As New ADOX.Column
champs.Name = "Ptr_" & nom1
champs.Type = ADOX.DataTypeEnum.adInteger
table.Columns.Append(champs)
champs = New ADOX.Column
champs.Name = "Ptr_" & nom2
champs.Type = ADOX.DataTypeEnum.adInteger
table.Columns.Append(champs)
Dim clef = New ADOX.Key
clef.Type = KeyTypeEnum.adKeyForeign
clef.Name = "clef_" & nom1 & "joint" & nom1 & nom2
clef.RelatedTable = nom1
clef.Columns.Append("Ptr_" & nom1)
clef.Columns("Ptr_" & nom1).RelatedColumn = "ID_" & nom1
table.Keys.Append(clef)
clef = New ADOX.Key
clef.Type = KeyTypeEnum.adKeyForeign
clef.Name = "clef_" & nom2 & "joint" & nom1 & nom2
clef.RelatedTable = nom2
clef.Columns.Append("Ptr_" & nom2)
clef.Columns("Ptr_" & nom2).RelatedColumn = "ID_" & nom2
table.Keys.Append(clef)
cat.Tables.Append(table)
End Sub
Protected Overrides Sub Finalize()
Deconnect()
MyBase.Finalize()
End Sub
End Class |
Partager