-
Notifications
You must be signed in to change notification settings - Fork 3
/
M_omSQLDataAdapter.def
128 lines (121 loc) · 4.25 KB
/
M_omSQLDataAdapter.def
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
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Compare Database
Option Explicit
Public Connection As New ADODB.Connection
Public Sub spExecuteNonQuery(StoredProcedureName As String, Optional Parameter1 As Variant = Null, Optional Parameter2 As Variant = Null, Optional Parameter3 As Variant = Null, Optional Parameter4 As Variant = Null, Optional Parameter5 As Variant = Null, Optional Parameter6 As Variant = Null)
Dim cmd As New ADODB.Command
Dim i As Long
cmd.CommandType = adCmdStoredProc
cmd.CommandText = StoredProcedureName
If Connection.State = 0 Then
Connection.Open
End If
Set cmd.ActiveConnection = Connection
cmd.Parameters.Refresh
' Load Parameters
For i = 1 To cmd.Parameters.Count - 1
Select Case i
Case 1
cmd.Parameters(i) = Parameter1
Case 2
cmd.Parameters(i) = Parameter2
Case 3
cmd.Parameters(i) = Parameter3
Case 4
cmd.Parameters(i) = Parameter4
Case 5
cmd.Parameters(i) = Parameter5
Case 6
cmd.Parameters(i) = Parameter6
End Select
Next
cmd.Execute
Connection.Close
Set cmd = Nothing
End Sub
Public Function spExecuteScalar(StoredProcedureName As String, Optional Parameter1 As Variant = Null, Optional Parameter2 As Variant = Null, Optional Parameter3 As Variant = Null, Optional Parameter4 As Variant = Null, Optional Parameter5 As Variant = Null, Optional Parameter6 As Variant = Null, Optional defaultValue As Variant = Null) As Variant
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
Dim i As Long
cmd.CommandType = adCmdStoredProc
cmd.CommandText = StoredProcedureName
If Connection.State = 0 Then
Connection.Open
End If
Set cmd.ActiveConnection = Connection
cmd.Parameters.Refresh
' Load Parameters
For i = 1 To cmd.Parameters.Count - 1
Select Case i
Case 1
cmd.Parameters(i) = Parameter1
Case 2
cmd.Parameters(i) = Parameter2
Case 3
cmd.Parameters(i) = Parameter3
Case 4
cmd.Parameters(i) = Parameter4
Case 5
cmd.Parameters(i) = Parameter5
Case 6
cmd.Parameters(i) = Parameter6
End Select
Next
Set rs = cmd.Execute
If Not rs.EOF Then
spExecuteScalar = Nz(rs(0), defaultValue)
Else
spExecuteScalar = defaultValue
End If
rs.Close
Set rs = Nothing
Connection.Close
Set cmd = Nothing
End Function
Public Function sqlOpenRecordset(SQLStatement As String) As ADODB.Recordset
Dim cmd As New ADODB.Command
cmd.CommandType = adCmdText
cmd.CommandText = SQLStatement
If Connection.State = 0 Then
Connection.Open
End If
Set cmd.ActiveConnection = Connection
Set sqlOpenRecordset = cmd.Execute
Set cmd = Nothing
End Function
Public Sub sqlExecute(SQLStatement As String)
Dim cmd As New ADODB.Command
cmd.CommandType = adCmdText
cmd.CommandText = SQLStatement
If Connection.State = 0 Then
Connection.Open
End If
Set cmd.ActiveConnection = Connection
cmd.Execute
Connection.Close
Set cmd = Nothing
End Sub
Public Sub CloseConnection()
Connection.Close
End Sub
Private Sub Class_Initialize()
Dim strDatabaseName As String
strDatabaseName = "Provider=MSDataShape.1;Data Provider=SQLOLEDB.1;Persist Security Info=True;SERVER=[SQLServer];DATABASE=[SQLDatabase];"
strDatabaseName = Replace(strDatabaseName, "[SQLServer]", "vamib-srv\vamos")
strDatabaseName = Replace(strDatabaseName, "[SQLDatabase]", "Vamos")
If Len("jara") > 0 Then
strDatabaseName = strDatabaseName & "UID=[SQLLogin];PWD=[SQLPassword];"
strDatabaseName = Replace(strDatabaseName, "[SQLLogin]", "vamos")
strDatabaseName = Replace(strDatabaseName, "[SQLPassword]", "79va20")
Else
strDatabaseName = strDatabaseName & "Trusted_Connection=yes;"
End If
Connection.ConnectionString = strDatabaseName
Connection.ConnectionTimeout = 0
End Sub
Private Sub Class_Terminate()
Set Me.Connection = Nothing
End Sub