本文介绍了SQL Server 到 VB.Net IF EXISTS 需要返回任何内容,但不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

IF EXISTS 
    (SELECT 1
    FROM User_Passwords
    WHERE up_Password = @CurrentPassword)
    PRINT 'Bad Password'
    --RETURN TRUE
ELSE

从我遇到的情况来看,我需要我的 SP 返回一些东西,一个 1 或 0,或者一个字符串/varchar,所以我可以在 VB 方面做一些工作.但截至目前,它没有返回任何内容,除非我没有正确调用它.这是VB代码:

From what I'm running into, I need my SP to return something, a 1 or 0, or a string/varchar, so I can do some work on the VB side. But as of right now, it's not returning anything, unless I'm not calling it correctly. This is the VB code:

Public Function UpdatePasswords()

    Dim objCommand As New SqlCommand("Update_Passwords", DatabaseInterface_.Connection)
    objCommand.CommandType = CommandType.StoredProcedure
    objCommand.Parameters.AddWithValue("@ua_pk", ua_pk_)
    objCommand.Parameters.Add(New SqlParameter With {.ParameterName = "@ResetDaysAmount", .SqlDbType = SqlDbType.Int, .Value = Company.GetSetting("PasswordExpireDays", SettingMisc.ReturnDataType.Integer_)})
    objCommand.Parameters.Add(New SqlParameter With {.ParameterName = "@AllowedNumberOfPasswords", .SqlDbType = SqlDbType.Int, .Value = Company.GetSetting("NumberOfPasswords", SettingMisc.ReturnDataType.Integer_)})
    objCommand.Parameters.Add(New SqlParameter With {.ParameterName = "@CurrentPasswordDate", .SqlDbType = SqlDbType.Date, .Value = ua_PasswordDate_})
    objCommand.Parameters.Add(New SqlParameter With {.ParameterName = "@CurrentPassword", .SqlDbType = SqlDbType.VarChar, .Value = ua_Password_})

    If objCommand.ExecuteScalar.ToString() = "Bad Password" Or 1 Then
        Return True
    Else
        Return False
    End If

End Function

推荐答案

你应该使用 SELECT 语句返回一些东西给你的代码

You should return something to your code using the SELECT statement

IF EXISTS (SELECT 1 FROM User_Passwords WHERE up_Password = @CurrentPassword)
    SELECT 1
ELSE
    SELECT 0

然后你的代码是

Dim result = Convert.ToInt32(objCommand.ExecuteScalar())
Return If(result = 1, True, False)

这篇关于SQL Server 到 VB.Net IF EXISTS 需要返回任何内容,但不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 12:02