数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

一组SQL Server身份验证管理子程序


发布日期:2021年07月21日
 
一组SQL Server身份验证管理子程序

Option Compare Database

Public appAccess As AccessApplication

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 srv As SQLDMOSQLServer

新建一个服务器实例

Set srv = New SQLDMOSQLServer

调用 SQL Server 登录连接方法

srvConnect srvname suid pwd

断开连接

srvDisconnect

Set srv = Nothing

End Sub

Sub CallSQLDMOWindowsLogin()

Dim srvname As String

设置 Windows 登录参数

srvname = (local)

SQLDMOWindowsLogin srvname

End Sub

Sub SQLDMOWindowsLogin(srvname As String)

Dim srv As SQLDMOSQLServer

新建一个服务器实例

Set srv = New SQLDMOSQLServer

在调用前设置 LoginSecure 属性为 True

使用服务名进行连接

srvLoginSecure = True

srvConnect srvname

断开连接

srvDisconnect

Set srv = 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 srv As SQLDMOSQLServer

指定哪个服务器默认为 Local (本地服务器)

srvname = (local)

使用集成安全(Windows)方式新建一个 SQL Server 对象并进行连接

Set srv = New SQLDMOSQLServer

srvLoginSecure = True

srvConnect srvname

设置 SecurityMode 属性为 Windows 或混合身份验证模式

srvIntegratedSecuritySecurityMode = constAuth

srvDisconnect

调用 Stop 方法停止服务器直到服务器完全停止

srvStop

Do Until srvStatus = SQLDMOSvc_Stopped

Loop

重新以混合模式启动服务器

srvStart True srvname

断开连接

srvDisconnect

Set srv = Nothing

End Sub

Sub ToWindowsAuthentication()

Dim srv As SQLDMOSQLServer

指定哪个服务器默认为 Local (本地服务器)

srvname = (local)

使用集成安全(Windows)方式新建一个 SQL Server 对象并进行连接

Set srv = New SQLDMOSQLServer

srvLoginSecure = True

srvConnect srvname

设置 SecurityMode 属性为 Windows 身份验证模式

srvIntegratedSecuritySecurityMode = SQLDMOSecurity_Integrated

srvDisconnect

调用 Stop 方法停止服务器直到服务器完全停止

srvStop

Do Until srvStatus = SQLDMOSvc_Stopped

Loop

重新以混合模式启动服务器

srvStart True srvname

断开连接

srvDisconnect

Set srv = Nothing

End Sub

Sub WindowsToMixedAuthentication()

Dim srv As SQLDMOSQLServer

指定哪个服务器默认为 Local (本地服务器)

srvname = (local)

使用集成安全(Windows)方式新建一个 SQL Server 对象并进行连接

Set srv = New SQLDMOSQLServer

srvLoginSecure = True

srvConnect srvname

设置 SecurityMode 属性混合身份验证模式

srvIntegratedSecuritySecurityMode = SQLDMOSecurity_Mixed

srvDisconnect

调用 Stop 方法停止服务器直到服务器完全停止

srvStop

Do Until srvStatus = SQLDMOSvc_Stopped

Loop

重新以混合模式启动服务器

srvStart True srvname

断开连接

srvDisconnect

Set srv = 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 = msdn

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 会话实例 (使用 : Access : Access )

Set appAccess = CreateObject(AccessApplication)

使用登录名和口令打开 ADP 并使其可视

strPrFilePath = prpath & prname

appAccessOpenAccessProject strPrFilePath

appAccessVisible = True

指定 ADP 使用的新的 Windwos 或 SQL Server 登录方式

If bolWindowsLogin Then

appAccessCurrentProjectOpenConnection _

PROVIDER=SQLOLEDB;INTEGRATED SECURITY=SSPI; & _

PERSIST SECURITY INFO=FALSE;INITIAL CATALOG= & _

dbname & ;DATA SOURCE= & srvname

Else

sConnectionString = PROVIDER=SQLOLEDB;INITIAL CATALOG= & _

dbname & ;DATA SOURCE= & srvname

appAccessCurrentProjectOpenConnection _

sConnectionString _

suid pwd

End If

按上面提出的要求关闭 Access 会话实例

If bolLeaveOpen = False Then

appAccessCloseCurrentDatabase

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 srv As SQLDMOSQLServer

上一篇:使用sqlserver来存放和取得session

下一篇:Whats new in Microsoft SQLServer2000(三)