可是我不知道怎样连接到SQL
'------------------------------------------
' 功能:进行服务器连接
' 参数:SQL 代查询的sql语句
' 输出:返回一个数据集对象
'------------------------------------------
Public Function ExecuteSQL(ByVal SQL As String) As ADODB.Recordset
Dim cnn As New ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo Error_Do
cnn.Open "Provider=SQLOLEDB.1;Password=kfp0107;Persist Security Info=True;User ID=sa;Initial Catalog=KFP_TXL;Data Source=我的电脑"
sTokens() = Split(SQL)
If InStr("INSERT,DELETE,UPDATE", UCase(sTokens(0))) Then
cnn.Execute SQL
Else
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = cnn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.Open Trim(SQL)
End With
'rst.Open Trim(SQL), cnn, adOpenStatic, adLockOptimistic
Set ExecuteSQL = rst
End If
Set cnn = Nothing
Set rst = Nothing
Exit Function
Error_Do:
Dim Err_Str As String
Err_Str = Err.Description
If MsgBox(Err_Str, vbRetryCancel + vbCritical, "警告") = vbRetry Then
Resume
Else
Set cnn = Nothing
Set rst = Nothing
End
End If
End Function