如何向 SQL Server 添加用户并设置用户权限
3个回答
展开全部
用户登录 id 与存储的过程 sp_addlogin 添加到 SQL Server。执行 sp_addlogin 权限只限于 SQL Server 系统管理员。
创建一个名 Userconn.prg,使用下面的代码的程序文件,请执行以下操作:
LPARAMETER cDSN,cUserName,cUserPassWord
*!* Connect to SQL Server as a restricted access user.
hConnect=SQLCONNECT(cDSN,cUserName,cUserPassWord)
IF hConnect >0
cSQLCommand="SELECT * FROM PUBS.DBO.AUTHORS"
gnExec = SQLEXEC(hConnect, cSQLCommand,'MYCURSOR')
? cSQLCommand,gnExec
IF gnExec>0
SELECT mycursor
BROW
ENDIF
*!* Disconnect as restricted access user.
=SQLDISCONNECT(hConnect)
ELSE
=MESSAGEBOX("Connection failed",0,"Connection Error")
ENDIF
RETURN
创建一个名 Adduser.prg,使用下面的代码的程序文件,请执行以下操作:
hConnect=SQLCONNECT(cDSN,cUserName,cUserPassWord)
IF hConnect >0
lUserExists=.F.
*!* Poll MASTER.DBO.SYSLOGINS for the NAME COLUMN.
cSQLCommand="SELECT NAME FROM MASTER.DBO.SYSLOGINS"
gnExec = SQLEXEC(hConnect, cSQLCommand,'SYSLOGS')
IF gnExec>0
SELECT syslogs
LOCA
SCAN FOR ALLTRIM(NAME)=ALLTRIM(cUserName)
lUserExists=.T.
EXIT
ENDSCAN
ENDIF
IF !lUserExists
*!* Select the PUBS database on SQL Server.
cSQLCommand="USE PUBS"
gnExec = SQLEXEC(hConnect,cSQLCommand)
*!* Add a new SQL Server Login ID cUserName.
*!* Access granted to the PUBS database.
cSQLCommand="EXEC sp_addlogin "+alltrim(cUserName)+"," + ;
alltrim(cUserPassWord)+",PUBS"
gnExec = SQLEXEC(hConnect, cSQLCommand)
*!* Grant privileges to user "cUserName".
cSQLCommand="GRANT SELECT ON pubs.dbo.authors " + ;
"TO "+cUserName
gnExec = SQLEXEC(hConnect, cSQLCommand)
gnCommit = SQLCOMMIT(hConnect)
ENDIF
*!* Disconnect as System Administrator.
=SQLDISCONNECT(hConnect)
ELSE
=MESSAGEBOX("Connection failed",0,"Connection Error")
ENDIF
RETURN
从命令窗口键入以下内容 (MyDsn 等于有效的 ODBC 数据源名称):
DO USERCONN WITH 'MyDsn','TEST','TEST'
出现以下错误消息:
失败的连接 SQL 状态:"28000"SQL Server 错误: 4002 [Microsoft] [ODBC SQL Server 驱动程序] [SQL Server] 登录失败
在命令窗口中键入以下 (MyDsn 等于有效的 ODBC 数据源名称):
DO ADDUSER WITH 'MyDsn','TEST','TEST'
在命令窗口中键入以下内容:
DO USERCONN WITH 'MyDsn','TEST','TEST'
一个游标,并使用 PUBS.DBO.AUTHORS 表中的数据将显示在一个浏览窗口。
创建一个名 Userconn.prg,使用下面的代码的程序文件,请执行以下操作:
LPARAMETER cDSN,cUserName,cUserPassWord
*!* Connect to SQL Server as a restricted access user.
hConnect=SQLCONNECT(cDSN,cUserName,cUserPassWord)
IF hConnect >0
cSQLCommand="SELECT * FROM PUBS.DBO.AUTHORS"
gnExec = SQLEXEC(hConnect, cSQLCommand,'MYCURSOR')
? cSQLCommand,gnExec
IF gnExec>0
SELECT mycursor
BROW
ENDIF
*!* Disconnect as restricted access user.
=SQLDISCONNECT(hConnect)
ELSE
=MESSAGEBOX("Connection failed",0,"Connection Error")
ENDIF
RETURN
创建一个名 Adduser.prg,使用下面的代码的程序文件,请执行以下操作:
hConnect=SQLCONNECT(cDSN,cUserName,cUserPassWord)
IF hConnect >0
lUserExists=.F.
*!* Poll MASTER.DBO.SYSLOGINS for the NAME COLUMN.
cSQLCommand="SELECT NAME FROM MASTER.DBO.SYSLOGINS"
gnExec = SQLEXEC(hConnect, cSQLCommand,'SYSLOGS')
IF gnExec>0
SELECT syslogs
LOCA
SCAN FOR ALLTRIM(NAME)=ALLTRIM(cUserName)
lUserExists=.T.
EXIT
ENDSCAN
ENDIF
IF !lUserExists
*!* Select the PUBS database on SQL Server.
cSQLCommand="USE PUBS"
gnExec = SQLEXEC(hConnect,cSQLCommand)
*!* Add a new SQL Server Login ID cUserName.
*!* Access granted to the PUBS database.
cSQLCommand="EXEC sp_addlogin "+alltrim(cUserName)+"," + ;
alltrim(cUserPassWord)+",PUBS"
gnExec = SQLEXEC(hConnect, cSQLCommand)
*!* Grant privileges to user "cUserName".
cSQLCommand="GRANT SELECT ON pubs.dbo.authors " + ;
"TO "+cUserName
gnExec = SQLEXEC(hConnect, cSQLCommand)
gnCommit = SQLCOMMIT(hConnect)
ENDIF
*!* Disconnect as System Administrator.
=SQLDISCONNECT(hConnect)
ELSE
=MESSAGEBOX("Connection failed",0,"Connection Error")
ENDIF
RETURN
从命令窗口键入以下内容 (MyDsn 等于有效的 ODBC 数据源名称):
DO USERCONN WITH 'MyDsn','TEST','TEST'
出现以下错误消息:
失败的连接 SQL 状态:"28000"SQL Server 错误: 4002 [Microsoft] [ODBC SQL Server 驱动程序] [SQL Server] 登录失败
在命令窗口中键入以下 (MyDsn 等于有效的 ODBC 数据源名称):
DO ADDUSER WITH 'MyDsn','TEST','TEST'
在命令窗口中键入以下内容:
DO USERCONN WITH 'MyDsn','TEST','TEST'
一个游标,并使用 PUBS.DBO.AUTHORS 表中的数据将显示在一个浏览窗口。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询