Option Compare Database
Public appAccess As Access.Application
Sub CallSQLDMOSQLServerLogin()
Dim srvname As String
Dim suid As String
Dim pwd As String
' 设置 SQL Server 的登录参数
srvname = "(local)"
'suid = "sa"
'pwd = ""
' 调用 SQL Server 登录过程
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 登录连接方法
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 身份认证模式的方法
ChangeServerAuthenticationMode constAuth
End Sub
Sub ChangeServerAuthenticationMode(constAuth As Byte)
Dim srv1 As SQLDMO.SQLServer
' 指定哪个服务器,默认为 Local (本地服务器)
srvname = "(local)"
' 使用集成安全(Windows)方式新建一个 SQL Server 对象并进行连接
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 对象并进行连接
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 对象并进行连接
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 Settings\Administrator\My Documents\" ' ADP 文件所在的磁盘位置
prname = "NorthwindCS" ' ADP 文件名
suid = "msdn5"
pwd = "password"
' 该参数用于控制当前用户使用 Windows 登录
' 代替 SQL Server 的 suid 和 pwd
bolWindowsLogin = False
' 使用 Windows 或 SQL Server 登录调用打开名为 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 登录方式
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 的参数
srvname = "(local)"
suid = "sa"
pwd = ""
' 调用 SQL Server 登录子程序
LoginDemo srvname, suid, pwd
End Sub
Sub LoginDemo(srvname As String, suid As String, pwd As String)
Dim srv1 As SQLDMO.SQLServer
文章来源于领测软件测试网 https://www.ltesting.net/
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073