Obtenir la valeur de return de la procédure stockée et l'utiliser dans le programme

Ceci est un suivi de cette question où ma procédure stockée est la suivante:

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'Check_Previous_Passwords') BEGIN PRINT 'Dropping Procedure Check_Previous_Passwords' DROP Procedure Check_Previous_Passwords END GO PRINT 'Creating Procedure Check_Previous_Passwords' GO CREATE Procedure Check_Previous_Passwords @ua_pk uniqueidentifier, @IncomingPassword varchar(25) AS DECLARE @Temp VARCHAR(25) DECLARE @IsSamePassword bit SET @Temp = (SELECT TOP 1 up_Password FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate DESC) IF (EXISTS (SELECT 1 FROM User_Passwords up WHERE up.ua_fk = @ua_pk AND @IncomingPassword = up.up_Password)) BEGIN SELECT @IsSamePassword = 1 END ELSE BEGIN SELECT @IsSamePassword = 0 END GO GRANT EXEC ON Check_Previous_Passwords TO WEB GO 

Je suis vraiment sûr que le SQL associé returnne une valeur. Mais quand je l'apporte dans ma solution VB.Net et l'utilise, cela indique que mon "Expression ne produit pas de valeur". Eh bien, voici mon code VB:

Sur le button cliquez:

 If user_.Check_Previous_Passwords(user_) = True Then 'throw error and set ResetPassword to true End If 

sur le User.vb

 Public Sub Check_Previous_Passwords(ByVal User As FoundationLibrary.User) Dim IsSamePassword_ As Integer Dim objCommand As New SqlCommand("Check_Previous_Passwords", DatabaseInterface_.Connection) objCommand.CommandType = CommandType.StoredProcedure objCommand.Parameters.AddWithValue("@ua_pk", ua_pk_) objCommand.Parameters.AddWithValue("@IncomingPassword", ua_Password_) DatabaseInterface_.Open() IsSamePassword_ = objCommand.ExecuteScalar DatabaseInterface_.Close() If IsSamePassword_ = 1 Then User.ua_ResetPassword_ = True Else User.ua_ResetPassword_ = False End If End Sub 

J'ai utilisé Dim IsSamePassword As Boolean , puis j'ai changé mon instruction If pour qu'elle soit égale à True . Toujours jeter la même erreur.

J'ai ce sentiment est vraiment facile.

Le SQL est plus compliqué qu'il ne devrait l'être – vous pourriez utiliser

 CREATE Procedure Check_Previous_Passwords @ua_pk uniqueidentifier, @IncomingPassword varchar(25) AS SELECT COUNT(*) FROM User_Passwords up WHERE up.ua_fk = @ua_pk AND @IncomingPassword = up.up_Password 

et le VB qui l'utilise doit être une fonction pour pouvoir returnner une valeur:

 Option Ssortingct On ' ...' Public Function Check_Previous_Passwords(ByVal User As FoundationLibrary.User) As Boolean Dim isSamePassword As Integer Dim objCommand As New SqlCommand("Check_Previous_Passwords", DatabaseInterface_.Connection) objCommand.CommandType = CommandType.StoredProcedure objCommand.Parameters.Add(New SqlParameter With {.ParameterName"@ua_pk", .SqlDbType = SqlDbType.UniqueIdentifier, .Value = ua_pk_}) objCommand.Parameters.Add(New SqlParameter With {.ParameterName = "@IncomingPassword", .SqlDbType = SqlDbType.VarChar, .Size = 25, .Value = ua_Password_}) DatabaseInterface_.Open() isSamePassword = CInt(objCommand.ExecuteScalar) DatabaseInterface_.Close() User.ua_ResetPassword_ = (isSamePassword = 1) Return User.ua_ResetPassword_ End Function 

J'ai changé les parties AddWithValue à une version qui fonctionne de manière fiable. Vous devriez éviter AddWithValue – cela ne vous apportera que de la misère: Pouvons-nous arrêter d'utiliser AddWithValue () déjà?

Votre procédure stockée ne renvoie aucune valeur. SELECT @IsSamePassword = 0 statement only let the value 0 dans la variable @IsSamePassword . Écrivez l' SELECT @IsSamePassword à la fin de votre procédure ou éliminez la variable @IsSamePassword .

 CREATE Procedure Check_Previous_Passwords @ua_pk uniqueidentifier, @IncomingPassword varchar(25) AS DECLARE @Temp VARCHAR(25) DECLARE @IsSamePassword bit SET @Temp = (SELECT TOP 1 up_Password FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate DESC) IF (EXISTS (SELECT 1 FROM User_Passwords up WHERE up.ua_fk = @ua_pk AND @IncomingPassword = up.up_Password)) BEGIN SELECT 1 END ELSE BEGIN SELECT 0 END 

Ou

 CREATE Procedure Check_Previous_Passwords @ua_pk uniqueidentifier, @IncomingPassword varchar(25) AS DECLARE @Temp VARCHAR(25) DECLARE @IsSamePassword bit SET @Temp = (SELECT TOP 1 up_Password FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate DESC) IF (EXISTS (SELECT 1 FROM User_Passwords up WHERE up.ua_fk = @ua_pk AND @IncomingPassword = up.up_Password)) BEGIN SELECT @IsSamePassword = 1 END ELSE BEGIN SELECT @IsSamePassword = 0 END SELECT @IsSamePassword 

Ceci est juste pour vous montrer plus de capacités et de techniques. En fait, vous pouvez vraiment utiliser RETURN dans votre SP

 . . . . . IF EXISTS (SELECT ... ) RETURN 1 ELSE RETURN 0 . . . . . 

Avec cela en place, vous devrez modifier l'appel VB pour inclure un paramètre de return à la fin, en utilisant la direction du paramètre

 p.Direction = ParameterDirection.ReturnValue