0

Asp.netメンバーシップ2.0。

テーブルaspnet_userのユーザー名を更新したいのですが、テーブルを直接更新するのが最善の方法です。

UPDATE aspnet_Users SET UserName = @UserName,LoweredUserName = LOWER( @UserName) 
WHERE UserName = @CurrentUname 

それがユニークであることを確認する方法は?

ありがとう、

アップデート: If not exist (select UserName from aspnet_Users) Begin UPDATE aspnet_Users SET UserName = @UserName,LoweredUserName = LOWER( @UserName)
WHERE UserName = @CurrentUname
End

上記のコードを変更するにはどうすればよいですか?

2番目の更新:

If not exist (select UserName from aspnet_Users WHERE UserName = @CurrentUname)
Begin 
  UPDATE aspnet_Users SET UserName = @UserName,LoweredUserName = LOWER( @UserName)
WHERE UserName = @CurrentUname
End
SELECT 1;
else
select 0;
4

3 に答える 3

4

ユーザー名列に一意のインデックスを作成します。

何かのようなもの:

CREATE UNIQUE INDEX index_username
ON aspnet_Users (UserName)
于 2012-04-17T17:53:03.377 に答える
3

データベースの列に一意性制約を設定します。次に、更新を試みる前に既存のユーザー名を確認するか、例外をキャッチしてそれに応じて処理します。

于 2012-04-17T17:53:28.550 に答える
1

@Icarusと@PeteMRepliesに加えて。これらはすでに良い/信頼できる提案です。

別の方法は、TransactionScopeを使用できますか?

擬似コード

using (TransactionScope scope = new TransactionScope())
{
    UPDATE aspnet_Users 
    SET    UserName = @UserName,
           LoweredUserName = LOWER( @UserName) 
    WHERE  UserName = @CurrentUname

    if((select count(userName) 
        From   aspnet_Users
        Where  userName = @CurrentUname) = 1)
    Begin
         scope.Complete
    End
}

上記のアプローチは、独立性session of the requestTransaction維持しisolated、他のユーザーによる他の要求からそれを守ります。

于 2012-04-17T18:25:01.757 に答える