|
To be honest, I think your code above is very redundant
ado and ado.net are not the same. In ado, it is generally used to open continuously, that is, it is OK to open it once, try not to close the connection, and keep switching the connection. The actual consumption is greater.
A lot of your code is useless. Modify a part, see the comment section
Option Explicit
'Define Connection and recordset as global, use local unless necessary for a small area
Public mycon As New ADODB.Connection
Public rst As New ADODB.Recordset
Public Function ExecuteSQL (ByVal sql As String) As ADODB.Recordset
Dim strConn As String 'Defines connection parameters
'Connetcion is assignment
strConn = "Provider = SQLOLEDB.1; Persist Security Info = False; User ID = sa; Password = e; Initial Catalog = tt; Data Source = (local)"
'Added the function to determine whether the Connection is normally connected
'Set mycon = New ADODB.Connection' is completely unnecessary, because you have already instantiated Connection above
'Use function to determine whether sql has been opened normally
If LoadConn = False Then Exit Function
Dim stokens () As String
stokens = Split (sql)
If InStr ("INSER, DELETE, UPDATE", UCase (stokens (0))) Then
Debug.Print sql 'First debug.print your sql to see if it is correct, and it is recommended to use a statement such as strsql instead of sql. The above judgment code does not know what conditions you wrote, it feels very messy
mycon.Execute sql
Else
'Set rst = New ADODB.Recordset is still redundant code
rst.Open Trim (sql), mycon, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
End If
End Function
'Set boolen to determine whether the connection is normal
Public Function LoadConn (strConn As String) As Boolean
On Error GoTo err:
mycon.CursorLocation = adUseClient
mycon.Open strConn
mycon.CommandTimeout = 5
mycon.Open
LoadConn = True
Debug.Print "Open normally"
err:
LoadConn = False
Debug.Print "Cannot open sql"
err.Clear
End Function |
|