-4

如何调用带有两个参数的 SQL 函数?我想记住一个变量中的返回值。

功能是:

CREATE  FUNCTION [CheckPwd](  @User nvarchar(50),  @Pwd nvarchar(50)) 
RETURNS int
AS
BEGIN
DECLARE @RES int
DECLARE @PWD_DB varbinary(255)

  IF (@User is not null and @User != '' and @Pwd is not null and @Pwd != '' )
begin
    SELECT  @PWD_DB = password FROM UserTable WHERE username = @User
        SET @PwdRES = pwdcompare(@Pwd, @PWD_DB )
end
  ELSE
        SET @PwdRES = 0

RETURN @RES
END
4

1 回答 1

2

这是一个带有存储过程的示例,使用纯 SQL 是相似的。

Public Function getFooID(param1 As Int32, param2 As String)As Int32
    Dim returnValue As Int32
    Using con = New SqlConnection(My.Settings.ConnectionString)
        Using cmd = New SqlCommand("dbo.Foo", con)
            cmd.CommandType = System.Data.CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@param1", param1)
            cmd.Parameters.AddWithValue("@param2", param2)
             Try
                con.Open()
                returnValue = DirectCast(cmd.ExecuteScalar(), Int32)
            Catch ex As Exception
                ' a good place to insert logging functionality '
                log.Error("Error in getFooId: " + ex.Message);
                ' note that Throw keeps the stacktrace unlike Throw ex '
                Throw
            End Try
        End Using
    End Using
    Return returnValue 
End Sub

SqlCommand.ExecuteScalar方法

于 2012-09-11T14:58:49.783 回答