请问各位大虾,在b中如何使用ado访问sqlserver,最好能把具体的代码给出来,希望各位能不吝赐教。
我是做了一个公共模块,在模块中用程序连接数据库,具体代码如下:但就是连接不上,不知道哪里错了
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectionString
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) + "query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'rst.MoveLast
'getRecordCount
Set ExecuteSQL = rst
MsgString = "查询到" + rst.RecordCount + "条记录"
End If
ExecuteSQL_Exit:
'清空数据集对象
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误" + Err.Description
Resume ExecuteSQL_Exit
End Function
Public Function ConnectionString() As String
ConnectionString = "Provider=SQLOLEDB.1;DataSource=H:\西译中学\data\studentsystem.MDF"
End Function