Dim strTbUserIfo As String
'连接数据库
On Error Resume Next
Err.Clear
Set cnn = New ADODB.Connection
With cnn
    .Provider = "Microsoft OLE DB Provider for SQL Server"
    .Properties("Data Source") = "rain"    'sql server服务器名
    .Properties("Initial Catalog") = "Library_system" '要打开的数据库名
    .Properties("User ID") = "sa"
    .Open
End With
If Err.Number <> 0 Then
    MsgBox "不能打开数据库,请确定数据库文件是否存在!", vbExclamation + vbOKOnly, "数据库连接错误"    '打开失败
    End
End If
'打开表user_info
strTbUserIfo = "select * from user_ifo"
Set rsUserIfo = New ADODB.Recordset
With rsUserIfo
    Set .ActiveConnection = cnn
    .LockType = adLockOptimistic
    .CursorType = adOpenKeyset
    .Open strTbUserIfo
End With

解决方案 »

  1.   

    楼主,你看一下下面的这个帖子,里面都是我测试过的函数:
    http://expert.csdn.net/Expert/topic/1099/1099599.xml?temp=.4131891其实我看了一下,ODBC和SQL Server的OLE DB连接SQL Server2000的函数,有以下几种:
    1、使用ODBC的OLE DB提供者打开连接(DSN方式)
    2、使用ODBC的OLE DB提供者打开连接(无DSN方式)
    3、使用SQL Server的OLE DB提供者打开连接
    4、使用SQL Server的OLE DB提供者打开可靠连接
    5、使用Connection对象的属性打开连接
    6、使用UDL文件与SQL Server进行连接(更多信息,可参考SQL Server2000的帮助)
    7、使用Data Link对话框来连接SQL Server