指导问题(角色管理) [英] Guid Problem (Role Management)

查看:76
本文介绍了指导问题(角色管理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有aspnet_Roles

i have aspnet_Roles

ROLID                           ROLNAME

   9acf74a8-d48b-4541-815e-695117a7a093    Admin
   88299f05-b2aa-41c2-81f5-69959777677c    AltinUye
   5712cd36-406c-490e-9da0-235fcec32186    NormalUye




我有我的aspnet_UsersInRoles





i have my aspnet_UsersInRoles


 USERID                                 ROLEID
fbd74e8b-f5e6-44a9-9fd4-1930beafb0e3    9acf74a8-d48b-4541-815e-695117a7a093
e64aa732-8631-46e6-90bb-0d2b882a21e3    88299f05-b2aa-41c2-81f5-69959777677c
241e204e-530e-4917-97b6-4a14f7057e1f    88299f05-b2aa-41c2-81f5-69959777677c
ca5999b8-f076-482d-9be4-709efd0ce550    88299f05-b2aa-41c2-81f5-69959777677c
0e6789fe-b6fd-4ac5-8da9-f8eb661ae31f    88299f05-b2aa-41c2-81f5-69959777677c



我的存储过程是



My stored procedure is

ALTER proc [dbo].[RolDuzenle]
(
@UserId uniqueidentifier,
@RolId   uniqueidentifier,
@Result int output
)
as
Declare @RecordControl  int 
Set @RecordControl=(select count(*) from dbo.aspnet_UsersInRoles where UserId=@UserId and RoleId=@RolId) 
update dbo.aspnet_UsersInRoles set RoleId=@RolId   where UserId=@UserId 
if(@RecordControl=0) 
begin 
set @Result=0
end
else
begin
set @Result=1
end
return @Result







Baglanti baglan = new Baglanti();

          SqlParameter[] paramdizi2 = new SqlParameter[3]
          {
              new SqlParameter(";@UserId",SqlDbType.UniqueIdentifier),
              new SqlParameter("@RolId",SqlDbType.UniqueIdentifier),
              new SqlParameter("@Result",SqlDbType.Int)

          };

          paramdizi2[0].Value = new Guid(TxtUserId.Text);
          paramdizi2[1].Value = new Guid(TxtRolId.Text);
          paramdizi2[2].Direction = ParameterDirection.Output;
          baglan.ExecuteNonQuery("RolDuzenle", CommandType.StoredProcedure, paramdizi2);
          if (paramdizi2[2].Value.ToString() == "0")
          {
              LabelDurum.Text = "Rol Updated";
          }
          else
          {
              LabelDurum.Text = "The role is already same";
          }


ı管理了这部分.ı做了但是


我想得到以下结果



ı managed this part.ı did it but;


i want to get the result below


Baglanti baglan = new Baglanti();

          SqlParameter[] paramdizi2 = new SqlParameter[3]
          {
              new SqlParameter("@UserId";,SqlDbType.UniqueIdentifier),
              new SqlParameter("@RolId";,SqlDbType.UniqueIdentifier),
              new SqlParameter("@Result",SqlDbType.Int)

          };

          paramdizi2[0].Value = new Guid(TxtUserId.Text);
          paramdizi2[1].Value = new Guid(TxtRolId.Text);
          paramdizi2[2].Direction = ParameterDirection.Output;
          baglan.ExecuteNonQuery("RolDuzenle";, CommandType.StoredProcedure, paramdizi2);
          if (paramdizi2[2].Value.ToString() == "0")
          {
              LabelDurum.Text = "Role updated";
          }
          else
          {
              LabelDurum.Text = "The table doesnt include this RoleId";
          }


如果ı使用RolDuzenle存储过程,但是ı没有得到此结果


if ı used RolDuzenle stored procedure but, ı dont arrive this result

        //if (paramdizi2[2].Value.ToString() == "0")
         {
             LabelDurum.Text = "Role updated";
         }
         else
         {
             LabelDurum.Text = "The aspnet_Roles table doesnt include this RoleId"
         }
//

İfı输入false角色ID我的程序失败.

İf ı enter false Role Id my program is failed.

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK__aspnet_Us__RoleI__52593CB8". The conflict occurred in database "TskYurt", table"dbo.aspnet_Roles, column 'RoleId';.
The statement has been terminated.




有可能吗?

我想要这个




IS IT POSSIBLE?

i want to this

if (paramdizi2[2].Value.ToString() == "0")
         {
             LabelDurum.Text = "Role updated";
         }
         else
         {
             LabelDurum.Text ="The aspnet_Roles table doesnt include this RoleId";
         }

推荐答案

在更新角色ID之前,您需要检查该角色是否存在,如果不存在则立即返回0,无需更新这种情况.从错误消息中可以看到,它给出了外键约束"错误,这是正确的行为.


这是修改后的SP.只需将更新语句移至else部分(当存在具有该角色的记录时):
Before updating the roleid you need to check if that role exists, if it doesn''t then return 0 immediately, no need of update statement in that case. As you can see from the error message, it gives a "Foreign Key Constraint" error which is correct behavior.


Here is the modified SP. Just moved the update statement to the else part (when there exist a record with that role):
ALTER proc [dbo].[RolDuzenle]
(
@UserId uniqueidentifier,
@RolId   uniqueidentifier,
@Result int output
)
as
Declare @RecordControl  int 
Set @RecordControl=(select count(*) from dbo.aspnet_UsersInRoles where UserId=@UserId and RoleId=@RolId)
begin 
set @Result=0
end
else
begin 
update dbo.aspnet_UsersInRoles set RoleId=@RolId   where UserId=@UserId 
if(@RecordControl=0) 
set @Result=1
end
return @Result



希望对您有所帮助.



I hope that helps.


这篇关于指导问题(角色管理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆