连接SQL数据库函数
只要先引用ADO,就可以使用下面的代码连接数据库啦,要修改一下数据库名,和服务器名就行啦,涵数不要修改用函数连接数据库
Public Const DataBaseName = "CC_BOOK" '数据库名称
Systitle = "书店管理系统"
If ConnSQL("CCTSOFT\SQLSEVER", DataBaseName, "sa", "") = False Then
MsgBox "连接数据库不成功!", vbExclamation, Systitle
Exit Sub
End If
‘连接数据库涵数
Public Function ConnSQL(ByVal cNtServerName As String, _
ByVal cDatabaseName As String, ByVal cSQLUserName As String, _
ByVal cSQLPassword As String) As Boolean '打开数据库
On Error GoTo SQLConErr
With DB
.CursorLocation = adUseClient
.Provider = "sqloledb"
.Properties("Data Source").Value = cNtServerName
.Properties("Initial Catalog").Value = cDatabaseName
.Properties("User ID") = cSQLUserName
.Properties("Password") = cSQLPassword
.Properties("prompt") = adPromptNever
'尝试打开时间,可以改这个时间
.ConnectionTimeout = 10
.Open
If .State = adStateOpen Then
ConnSQL = True
Exit Function
Else
ConnSQL = False
Set DB = Nothing
Exit Function
End If
End With
Exit Function
SQLConErr:
ConnSQL = False
Set DB = Nothing
End Function