权限管理----角色管理
下面示图为角色管理介面。在数据表[Role]中,最少需要两个字段[RoleId]和[RoleName]。Project中所有角色在此介面进行管理。
下面为表[Role]结构,直接拷贝在sql Server2008查询分析器执行:
代码
ET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Role](
[RoleId] [smallint] IDENTITY(1,1) NOT NULL,
[RoleName] [nvarchar](50) NOT NULL,
[Description] [nvarchar](100) NOT NULL,
PRIMARY KEY CLUSTERED
(
[RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UK_Role_UD980sA6] UNIQUE NONCLUSTERED
(
[RoleName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Role](
[RoleId] [smallint] IDENTITY(1,1) NOT NULL,
[RoleName] [nvarchar](50) NOT NULL,
[Description] [nvarchar](100) NOT NULL,
PRIMARY KEY CLUSTERED
(
[RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UK_Role_UD980sA6] UNIQUE NONCLUSTERED
(
[RoleName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
插入的存储过程usp_Role_Insert:
代码
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Role_Insert]
(
@RoleName nvarchar(50),
@Description nvarchar(50)
)
AS
SELECT [RoleName] FROM [Role] WHERE [RoleName]=@RoleName
IF @@ROWCOUNT>0
BEGIN
RAISERROR(N'此角色已经存在,无法添加!',16,1)
RETURN
END
ELSE
BEGIN TRANSACTION
DECLARE @err int
INSERT INTO dbo.Role([RoleName],[Description]) VALUES (@RoleName,@Description)
SET @err=@@ERROR
IF @err<>0
BEGIN
ROLLBACK TRANSACTION
END
COMMIT TRANSACTION
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Role_Insert]
(
@RoleName nvarchar(50),
@Description nvarchar(50)
)
AS
SELECT [RoleName] FROM [Role] WHERE [RoleName]=@RoleName
IF @@ROWCOUNT>0
BEGIN
RAISERROR(N'此角色已经存在,无法添加!',16,1)
RETURN
END
ELSE
BEGIN TRANSACTION
DECLARE @err int
INSERT INTO dbo.Role([RoleName],[Description]) VALUES (@RoleName,@Description)
SET @err=@@ERROR
IF @err<>0
BEGIN
ROLLBACK TRANSACTION
END
COMMIT TRANSACTION
最后即是更新的存储过程usp_Role_Update:
代码
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Role_Update]
(
@ID tinyint,
@RoleName nvarchar(50),
@Description nvarchar(50)
)
AS
SELECT [RoleName] FROM [Role] WHERE [RoleName]=@RoleName AND [RoleId] <> @ID
IF @@ROWCOUNT>0
BEGIN
RAISERROR(N'此角色已经存在,无法更新!',16,1)
RETURN
END
ELSE
BEGIN TRANSACTION
DECLARE @err int
UPDATE [Role] SET [RoleName]=@RoleName,[Description]=@Description WHERE [RoleId]=@ID
SET @err=@@ERROR
IF @err<>0
BEGIN
ROLLBACK TRANSACTION
END
COMMIT TRANSACTION
GO
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Role_Update]
(
@ID tinyint,
@RoleName nvarchar(50),
@Description nvarchar(50)
)
AS
SELECT [RoleName] FROM [Role] WHERE [RoleName]=@RoleName AND [RoleId] <> @ID
IF @@ROWCOUNT>0
BEGIN
RAISERROR(N'此角色已经存在,无法更新!',16,1)
RETURN
END
ELSE
BEGIN TRANSACTION
DECLARE @err int
UPDATE [Role] SET [RoleName]=@RoleName,[Description]=@Description WHERE [RoleId]=@ID
SET @err=@@ERROR
IF @err<>0
BEGIN
ROLLBACK TRANSACTION
END
COMMIT TRANSACTION
GO