无法从aspnet_Users或AspNetUsers中删除?

时间:2015-03-17 13:46:50

标签: sql asp.net sql-server localdb

我使用aspnet_regsql进行数据库设置,我正在使用visual studio 2013附带的Web表单模板。我试图从aspnet_Users和AspNetUsers中删除。但是,我需要使用存储过程,因为我从两个不同的表中删除。我可以从AspNetUsers中删除用户,但不能删除aspnet_Users。我在存储过程中做错了什么不能正常工作?

 CREATE PROCEDURE [dbo].[DeleteAllUserReferences]
        @UserName  NVARCHAR(256),
        @Id  NVARCHAR(128),
        @Role NVARCHAR(256),
        @RowsAffected int OUTPUT
    AS BEGIN TRY
        SET NOCOUNT ON                                                 
        SET XACT_ABORT ON  
        EXEC aspnet_Users_DeleteUser 'mainwebsite',@UserName,15,@RowsAffected output
       BEGIN TRANSACTION
        DELETE FROM [AspNetUsers] WHERE Id = @Id
       COMMIT TRANSACTION
    END TRY

    BEGIN CATCH
       IF @@trancount > 0 
       ROLLBACK TRANSACTION                        
       EXEC error_handler_sp                                          
       RETURN 55555 

    END CATCH

    RETURN 0

注意:aspnet_Users_DeleteUser在我调用它时没有在asp.net中执行,但是在visual studio中从SQL Server资源管理器手动完成。编辑:我忘了下面的错误处理程序:

CREATE PROCEDURE error_handler_sp AS

DECLARE @errmsg   nvarchar(2048),
        @severity tinyint,
        @state    tinyint,
        @errno    int,
        @proc     sysname,
        @lineno   int

SELECT @errmsg = error_message(), @severity = error_severity(),   -- 10
       @state  = error_state(), @errno = error_number(),
       @proc   = error_procedure(), @lineno = error_line()

IF @errmsg NOT LIKE '***%'                                        -- 11  
BEGIN 
   SELECT @errmsg = '*** ' + coalesce(quotename(@proc), '<dynamic SQL>') + 
                    ', ' + ltrim(str(@lineno)) + '. Errno ' + 
                    ltrim(str(@errno)) + ': ' + @errmsg
   RAISERROR(@errmsg, @severity, @state)
END
ELSE
   RAISERROR(@errmsg, @severity, @state)

1 个答案:

答案 0 :(得分:0)

原来我需要将UserName字段添加到listview的DataKeyNames属性并且它可以工作!