如何在VB中调用SQL函数。净

时间:2012-09-11 14:53:25

标签: sql vb.net function ado.net

如何调用带有两个参数的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

1 个答案:

答案 0 :(得分: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 Method

相关问题