标题:连接SQL数据库函数
只看楼主
海凌
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-8-1
 问题点数:0 回复次数:0 
连接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
2006-08-06 09:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-82368-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.126600 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved