Public Function GetConnStr() As String
Dim ConnString As String
ConnString = "Provider = sqlOLEDB.1;Database=IHURALibrary;server=.;uid=Lindy;pwd=sa!12345;"
GetConnStr = ConnString
End Function
Public Function OpenConn(ByRef Conn As ADODB.Connection) As Boolean
Set Conn = New ADODB.Connection
On Error GoTo ErrorHandle
Conn.Open GetConnStr
OpenConn = True
Exit Function
ErrorHandle:
MsgBox "Á¬½ÓÊý¾Ý¿âʧ°Ü£¡ÇëÖØÐÂÁ¬½Ó"
OpenConn = False
Exit Function
End Function
Public Function Executesql(ByVal sql As String,ByRef msg As String) As String
Dim Conn As ADODB.Connection
On Error GoTo ErrorHandle
If OpenConn(Conn) Then
Conn.Execute sql
msg = "²Ù×÷Ö´Ðгɹ¦£¡"
End If
Executesql = ""
Exit Function
ErrorHandle:
msg = "Ö´ÐдíÎó£º" & Err.Description
Executesql = ""
Set Conn = Nothing
Exit Function
End Function
Public Function Selectsql(ByVal sql As String,ByRef msg As String) As ADODB.Recordset
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ErrorHandle
sTokens = Split(sql)
If InStr("SELECT",UCase((sTokens(0)))) Then
If OpenConn(Conn) Then
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open Trim$(sql),Conn,adOpenDynamic,adLockOptimistic
Set Selectsql = rs
msg = "²éѯµ½" & rs.RecordCount & " Ìõ¼Ç¼!"
End If
Else
msg = "sqlÓï¾äÓÐÓï:" & sql
End If
ErrorHandle:
msg = "²éѯ´íÎó£º" & Err.Description
Set rs = Nothing
Set Conn = Nothing
Exit Function
End Function
Public Function BatchSelectsql(ByVal sql As String,ByRef msg As String) As ADODB.Recordset Dim Conn As ADODB.Connection Dim rs As ADODB.Recordset Dim sTokens() As String On Error GoTo ErrorHandle sTokens = Split(sql) If InStr("SELECT",UCase((sTokens(0)))) Then If OpenConn(Conn) Then Set rs = New ADODB.Recordset rs.CursorLocation = adUseClient rs.Open Trim$(sql),adLockBatchOptimistic Set Selectsql = rs msg = "²éѯµ½" & rs.RecordCount & " Ìõ¼Ç¼!" End If Else msg = "sqlÓï¾äÓÐÓï:" & sql End If ErrorHandle: msg = "²éѯ´íÎó£º" & Err.Description Err.Description Set rs = Nothing Set Conn = Noting Exit Function End Function
原文链接:https://www.f2er.com/vb/260185.html