Public Function exec_procedure(sql As String, Optional i As Integer = 0) As String
exec_procedure = ""
If sql = "" Then Exit Function
Dim cnn1 As ADODB.Connection
Dim rst1 As ADODB.Recordset
Set cnn1 = New ADODB.Connection
Call cnn1.Open(connectstr())
Set rst1 = New ADODB.Recordset
rst1.ActiveConnection = cnn1
On Error GoTo errorhandle
Call rst1.Open(sql, cnn1, adOpenKeyset, adLockReadOnly)
If i <> 555 And i <> 999 Then
If rst1.RecordCount = -1 Then exec_procedure = CStr(rst1.Fields(i).value)
If rst1.RecordCount = 1 Then exec_procedure = CStr(rst1.Fields(i).value)
End If
If i = 999 Then
ReDim arrary_rpt(rst1.Fields.Count) As String
If (rst1.RecordCount = 1 Or rst1.RecordCount = -1) And rst1.Fields.Count >= 1 Then
Dim k As Integer
For k = 0 To rst1.Fields.Count - 1
arrary_rpt(k) = rst1.Fields(k).value
Next
Else
For k = 0 To rst1.Fields.Count - 1
arrary_rpt(k) = ""
Next
End If
End If
Set rst1 = Nothing
cnn1.Close
Set cnn1 = Nothing
Exit Function
errorhandle:
Err.Clear
Set rst1 = Nothing
Set cnn1 = Nothing
Exit Function
End Function
Private Sub Form_Load()
Dim henv As Long
Dim hdbc As Long
Dim hstmt As Long
Dim rc As Integer
Dim dsn As String
Dim pwd As String
Dim uid As String
Dim rs As String
dsn = "SDKH"
pwd = "123"
uid = "SDKH"
rc = SQLAllocEnv(henv)
rc = SQLAllocConnect(henv, hdbc)
rc = SQLConnect(hdbc, dsn, Len(dsn), uid, Len(uid), pwd, Len(pwd))
If rc <> SQL_SUCCESS Then
MsgBox "connect failed"
Exit Sub
End If
rc = SQLAllocStmt(hdbc, hstmt)
rs = "insert into agentest values(2004,'me','普通作息',124)"
rc = SQLExecDirect(hstmt, rs, Len(rs)) //rc=-1
If rc = SQL_SUCCESS Then
MsgBox "insert success"
Else
MsgBox Err.LastDllError //output 0
MsgBox "insert failed"
End If