我无法将我的SQL用户函数的结果返回到我的vb.net程序

时间:2013-06-14 19:57:04

标签: sql vb.net function call oledb

我把头发拉出来了!!

我编写了一个简单的标量函数,现在我想从vb.net执行它并获得结果,但它总是显示为零,即使我在SSMS中调用它返回的值为1,这就是我的期望。

该功能如下所示:

Alter FUNCTION [dbo].[CheckParity]
(@test char(1)  )
RETURNS int
AS
BEGIN
declare @result int
IF (SELECT SUM(REVENUEAMOUNT) FROM CommissionDetail WHERE  RevenueType IN('P','S')) =
   (SELECT SUM(SERVICEREVENUE + PRODUCTREVENUE) FROM SalesmanAttainment)
    set @result = 1
else  
    set @result = 0
return @result
END

我从网上抓取了vb代码:

                sql = "dbo.CheckSalesAttainmentParity"
                Dim count As Integer

                cnn = New OleDbConnection(conn.ConnectionString)
                Try
                    cnn.Open()
                    cmd = New OleDbCommand(sql, cnn)
                    cmd.Parameters.Add("test", OleDbType.Integer)
                    cmd.Parameters("test").Direction = ParameterDirection.ReturnValue
                    cmd.ExecuteScalar()
                    count = cmd.Parameters("test").Value
                    cmd.Dispose()
                    cnn.Close()                   
                sql = "dbo.CheckSalesAttainmentParity"
                Dim count As Integer

                cnn = New OleDbConnection(conn.ConnectionString)
                Try
                    cnn.Open()
                    cmd = New OleDbCommand(sql, cnn)
                    cmd.Parameters.Add("test", OleDbType.Integer)
                    cmd.Parameters("test").Direction = ParameterDirection.ReturnValue
                    cmd.ExecuteScalar()
                    count = cmd.Parameters("test").Value
                    cmd.Dispose()
                    cnn.Close()

我完全难过了。我很欣赏所有的想法

2 个答案:

答案 0 :(得分:1)

咄!

我遗漏了这行代码:

cmd.CommandType = CommandType.StoredProcedure

答案 1 :(得分:0)

您正在使用SQL Server数据库,因此我不确定您是否使用OleDbConnection而不是SqlConnection,但请尝试此操作。

    Dim Sql As String = "select Sample.dbo.CheckParity('a')"
    Dim count As Integer
    Dim cnn As SqlConnection
    Dim cmd As SqlCommand

    Try
        cnn = New SqlConnection(conn.ConnectionString)
        cnn.Open()

        cmd = New SqlCommand(Sql, cnn)

        count = CInt(cmd.ExecuteScalar())

        Console.WriteLine(count)
        Console.ReadKey()
    Finally
        cmd.Dispose()
        cnn.Close()
    End Try