这是this question的后续操作,我的存储过程是这样的:
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
我确定相关的SQL正在返回一个值。但是,当我将它带入我的VB.Net解决方案并使用它时,它说明我的"表达式不会产生值"。那么这是我的VB代码:
点击按钮:
If user_.Check_Previous_Passwords(user_) = True Then
'throw error and set ResetPassword to true
End If
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
我使用了Dim IsSamePassword As Boolean
,然后将If
语句更改为等于True
。仍然抛出相同的错误。
我有这种感觉真的很容易。
答案 0 :(得分:4)
您的存储过程不会返回任何值。 SELECT @IsSamePassword = 0 statement only let the value
0到@IsSamePassword
变量。将SELECT @IsSamePassword
语句写入您的过程结尾或删除@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
或者
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
答案 1 :(得分:2)
SQL比它需要的更复杂 - 你可以使用
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
并且使用它的VB需要是一个函数,以便它可以返回一个值:
Option Strict 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
我将AddWithValue
部分更改为可靠运行的版本。你应该避免AddWithValue
- 它最终只会给你带来痛苦:Can we stop using AddWithValue() already?
答案 2 :(得分:1)
这只是为了向您展示更多功能和技巧。事实上,您可以在SP中真正使用RETURN
. . . . .
IF EXISTS (SELECT ... )
RETURN 1
ELSE
RETURN 0
. . . . .
有了这个,你需要修改VB调用,最后在参数方向包含一个返回参数
p.Direction = ParameterDirection.ReturnValue