-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclsDBContext.cls
More file actions
93 lines (78 loc) · 2.44 KB
/
Copy pathclsDBContext.cls
File metadata and controls
93 lines (78 loc) · 2.44 KB
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsDBContext"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'@Folder "2-Infraestructura.4-Servicios.DBs"
Option Explicit
Private Const MODULE_NAME As String = "clsDBContext"
Private m_Cnn As Object ' ADODB.Connection
Public Sub Conectar(ByVal RutaBD As String)
Select Case True
Case Len(RutaBD) = 0, Dir(RutaBD) = ""
Err.Raise Err.Number, , _
"Error ADO al conectar a DB." & vbCrLf & _
"Path a la DB inexistente:" & vbCrLf & RutaBD
Exit Sub
End Select
Set m_Cnn = CreateObject("ADODB.Connection")
' If Not EstaConectado Then
' Err.Raise Err.Number, , "Error ADO, no hay conexion a la DB."
' Exit Sub
' End If
On Error GoTo ErrorHandler
m_Cnn.Open "Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" & RutaBD
On Error GoTo 0
Exit Sub
ErrorHandler:
Err.Raise Err.Number, , _
"Error ADO al abrir conexión a DB: " & vbCrLf & _
Err.Description
End Sub
Public Function EstaConectado() As Boolean
EstaConectado = Not m_Cnn Is Nothing And m_Cnn.State = 1
End Function
Public Sub Desconectar()
If Not m_Cnn Is Nothing Then
If m_Cnn.State = 1 Then m_Cnn.Close
Set m_Cnn = Nothing
End If
End Sub
Public Function AbrirRecordset(ByVal Sql As String) As Object
Select Case True
Case Sql = ""
Err.Raise Err.Number, , _
"Error ADO al abrir Recordset." & vbCrLf & _
"Function SQL inexistente"
Exit Function
End Select
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
If Not EstaConectado Then
Err.Raise Err.Number, , "Error ADO, no hay conexion a la DB."
Exit Function
End If
On Error GoTo ErrHandler
rs.Open Sql, m_Cnn, 1, 1 ' adOpenKeyset, adLockReadOnly
Set AbrirRecordset = rs
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[AbrirRecordset] SQL ejecutado: " & Sql
Err.Raise Err.Number, , _
"Error ADO al abrir Recordset." & vbCrLf & _
"SQL ejecutado:" & vbCrLf & Sql & vbCrLf & _
Err.Description
End Function
Public Sub BeginTrans()
m_Cnn.BeginTrans
End Sub
Public Sub CommitTrans()
m_Cnn.CommitTrans
End Sub
Public Sub RollbackTrans()
m_Cnn.RollbackTrans
End Sub