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
Dim lgn1 As SQLDMO.Login
'' 新建一服务器实例
Set srv1 = New SQLDMO.SQLServer
'' 以 SQL Server 方式连接 SQL Server
srv1.Connect srvname, suid, pwd
'' 新建登录对象
Set lgn1 = New SQLDMO.Login
'' 添加默认 SQL Server 登录 msdn6
lgn1.Name = "UserX"
lgn1.Database = "NorthwindCS"
lgn1.SetPassword "", "password"
srv1.Logins.Add lgn1
''重新新建登录对象
Set lgn1 = New SQLDMO.Login
'' 添加基于 Windows 组 SQL_users 的登录对象
lgn1.Name = "MYDESK\UserSQL"
lgn1.Database = "NorthwindCS"
lgn1.Type = SQLDMOLogin_NTGroup
srv1.Logins.Add lgn1
'' 在添加登录后,列出登录列表
Debug.Print "添加 2 个新登录后的登录列表:"
For Each lgn1 In srv1.Logins
Debug.Print DecodeLoginType(lgn1.Type), lgn1.Name
Next lgn1
'' 删除刚新建的登录
srv1.Logins.Remove "MYDESK\UserSQL"
srv1.Logins.Remove "UserX"
'' 列出登录列表
Debug.Print vbCr & "删除 2 个新登录后的登录列表:"
For Each lgn1 In srv1.Logins
Debug.Print DecodeLoginType(lgn1.Type), lgn1.Name
Next lgn1
'' 断开连接
srv1.Disconnect
Set srv1 = Nothing
End Sub
Function DecodeLoginType(lgn_type As Byte) As String
Select Case lgn_type
Case 0
DecodeLoginType = "SQLDMOLogin_NTUser"
Case 1
DecodeLoginType = "SQLDMOLogin_NTGroup"
Case 2
DecodeLoginType = "SQLDMOLogin_Standard"
Case Else
&