一组SQLServer身份验证管理子程序
2007-11-11 08:59:17 来源:WEB开发网核心提示:Option Compare DatabasePublic appAccess As Access.ApplicationSub CallSQLDMOSQLServerLogin()Dim srvname As StringDim suid As StringDim pwd As String' 设置 sql
Option Compare Database
Public appAccess As Access.Application
Sub CallSQLDMOSQLServerLogin()
Dim srvname As String
Dim suid As String
Dim pwd As String
' 设置 sql server(WINDOWS平台上强大的数据库平台) 的登录参数
srvname = "(local)"
'suid = "sa"
'pwd = ""
' 调用 sql server(WINDOWS平台上强大的数据库平台) 登录过程
SQLDMOSQLServerLogin srvname, suid, pwd
End Sub
Sub SQLDMOSQLServerLogin(srvname As String, suid As String, pwd As String)
Dim srv1 As SQLDMO.SQLServer
' 新建一个服务器实例
Set srv1 = New SQLDMO.SQLServer
' 调用 sql server(WINDOWS平台上强大的数据库平台) 登录连接方法
srv1.Connect srvname, suid, pwd
' 断开连接
srv1.Disconnect
Set srv1 = Nothing
End Sub
Sub CallSQLDMOWindowsLogin()
Dim srvname As String
' 设置 Windows 登录参数
srvname = "(local)"
SQLDMOWindowsLogin srvname
End Sub
Sub SQLDMOWindowsLogin(srvname As String)
Dim srv1 As SQLDMO.SQLServer
' 新建一个服务器实例
Set srv1 = New SQLDMO.SQLServer
' 在调用前,设置 LoginSecure 属性为 True
' 使用服务名进行连接
srv1.LoginSecure = True
srv1.Connect srvname
' 断开连接
srv1.Disconnect
Set srv1 = Nothing
End Sub
Sub CallChangeServerAuthenticationMode()
Dim constAuth As Byte
' 设置 constAuth 参数为:
' SQLDMOSecurity_Integrated 为 Windows Authentication 模式
' SQLDMOSecurity_Mixed 为 Mixed Authentication 模式
' 设置 constAuth 的默认值
constAuth = SQLDMOSecurity_Mixed
' 调用改变 sql server(WINDOWS平台上强大的数据库平台) 身份认证模式的方法
ChangeServerAuthenticationMode constAuth
End Sub
Sub ChangeServerAuthenticationMode(constAuth As Byte)
Dim srv1 As SQLDMO.SQLServer
' 指定哪个服务器,默认为 Local (本地服务器)
srvname = "(local)"
' 使用集成安全(Windows)方式新建一个 sql server(WINDOWS平台上强大的数据库平台) 对象并进行连接
Set srv1 = New SQLDMO.SQLServer
srv1.LoginSecure = True
srv1.Connect srvname
' 设置 SecurityMode 属性为 Windows 或混合身份验证模式
srv1.IntegratedSecurity.SecurityMode = constAuth
srv1.Disconnect
' 调用 Stop 方法停止服务器,直到服务器完全停止
srv1.Stop
Do Until srv1.Status = SQLDMOSvc_Stopped
Loop
' 重新以混合模式启动服务器
srv1.Start True, srvname
' 断开连接
srv1.Disconnect
Set srv1 = Nothing
End Sub
Sub ToWindowsAuthentication()
Dim srv1 As SQLDMO.SQLServer
' 指定哪个服务器,默认为 Local (本地服务器)
srvname = "(local)"
' 使用集成安全(Windows)方式新建一个 sql server(WINDOWS平台上强大的数据库平台) 对象并进行连接
Set srv1 = New SQLDMO.SQLServer
srv1.LoginSecure = True
srv1.Connect srvname
' 设置 SecurityMode 属性为 Windows 身份验证模式
srv1.IntegratedSecurity.SecurityMode = SQLDMOSecurity_Integrated
srv1.Disconnect
' 调用 Stop 方法停止服务器,直到服务器完全停止
srv1.Stop
Do Until srv1.Status = SQLDMOSvc_Stopped
Loop
' 重新以混合模式启动服务器
srv1.Start True, srvname
' 断开连接
srv1.Disconnect
Set srv1 = Nothing
End Sub
Sub WindowsToMixedAuthentication()
Dim srv1 As SQLDMO.SQLServer
' 指定哪个服务器,默认为 Local (本地服务器)
srvname = "(local)"
' 使用集成安全(Windows)方式新建一个 sql server(WINDOWS平台上强大的数据库平台) 对象并进行连接
Set srv1 = New SQLDMO.SQLServer
srv1.LoginSecure = True
srv1.Connect srvname
' 设置 SecurityMode 属性混合身份验证模式
srv1.IntegratedSecurity.SecurityMode = SQLDMOSecurity_Mixed
srv1.Disconnect
' 调用 Stop 方法停止服务器,直到服务器完全停止
srv1.Stop
Do Until srv1.Status = SQLDMOSvc_Stopped
Loop
' 重新以混合模式启动服务器
srv1.Start True, srvname
' 断开连接
srv1.Disconnect
Set srv1 = Nothing
End Sub
Sub CallOpenADPWindowsOrSQLServer()
Dim srvname As String
Dim dbname As String
Dim prpath As String
Dim prname As String
Dim suid As String
Dim pwd As String
Dim bolWindowsLogin As Boolean
' 设置打开 ADP 程序的参数
srvname = "(local)"
dbname = "NorthwindCS" ' ADP 连接的数据库
prpath = "C:Documents and SettingsAdministratorMy Documents" ' ADP 文件所在的磁盘位置
prname = "NorthwindCS" ' ADP 文件名
suid = "msdn5"
pwd = "password"
' 该参数用于控制当前用户使用 Windows 登录
' 代替 sql server(WINDOWS平台上强大的数据库平台) 的 suid 和 pwd
bolWindowsLogin = False
' 使用 Windows 或 sql server(WINDOWS平台上强大的数据库平台) 登录调用打开名为 prname ADP 的子程序
OpenADPWindowsOrSQLServer srvname, dbname, prpath, prname, suid, pwd, bolWindowsLogin
End Sub
Sub OpenADPWindowsOrSQLServer(srvname As String, dbname As String, _
prpath As String, prname As String, _
suid As String, pwd As String, bolWindowsLogin As Boolean)
Dim bolLeaveOpen As Boolean
Dim strPrFilePath As String
Dim sConnectionString As String
' 是否保持现有打开的程序?
If MsgBox("在该过程中是否关闭打开的 ADP?", vbYesNo) = vbYes Then
bolLeaveOpen = True
End If
' 新建 Access 会话实例 (使用 .9 : Access 2000, .10 : Access 2002)
Set appAccess = CreateObject("Access.Application.9")
' 使用登录名和口令打开 ADP 并使其可视
strPrFilePath = prpath & prname
appAccess.OpenAccessProject strPrFilePath
appAccess.Visible = True
' 指定 ADP 使用的新的 Windwos 或 sql server(WINDOWS平台上强大的数据库平台) 登录方式
If bolWindowsLogin Then
appAccess.CurrentProject.OpenConnection _
"PROVIDER=SQLOLEDB.1;INTEGRATED SECURITY=SSPI;" & _
"PERSIST SECURITY INFO=FALSE;INITIAL CATALOG=" & _
dbname & ";DATA SOURCE=" & srvname
Else
sConnectionString = "PROVIDER=SQLOLEDB.1;INITIAL CATALOG=" & _
dbname & ";DATA SOURCE=" & srvname
appAccess.CurrentProject.OpenConnection _
sConnectionString, _
suid, pwd
End If
' 按上面提出的要求关闭 Access 会话实例
If bolLeaveOpen = False Then
appAccess.CloseCurrentDatabase
Set appAccess = Nothing
End If
End Sub
Sub CallLoginDemo()
Dim srvname As String
Dim suid As String
Dim pwd As String
' 设置登录 sql server(WINDOWS平台上强大的数据库平台) 的参数
srvname = "(local)"
suid = "sa"
pwd = ""
' 调用 sql server(WINDOWS平台上强大的数据库平台) 登录子程序
LoginDemo srvname, suid, pwd
End Sub
Sub LoginDemo(srvname As String, suid As String, pwd As String)
Dim srv1 As SQLDMO.SQLServer