ASP.Net VB存储过程允许空参数 - 如何将NULL整数传递给存储过程

时间:2012-04-02 15:32:32

标签: asp.net vb.net stored-procedures null parameter-passing

ABC的值(参数)可以为NULL。我正在努力将其设置为null。以下声明无效。如果我尝试设置类型Nullable(OfInteger),我得到的错误是该值是'错误的类型'。提前谢谢。

 Dim ABC as Integer = Nothing

 Public Function RunStoredProce(ByVal **ABC** As Integer, ByVal DEF As String) _
               As IEnumerable(Of MyProjectStoredProcResults) Implements MyFunction.GetThatDataResult

        Dim y = (From p In dc.uspReportMyFunction(**ABC**, DEF)
            Select p).ToList

        Return y

    End Function

2 个答案:

答案 0 :(得分:0)

Dim ABC As Nullable(Of Integer)

Public Function RunStoredProce(ByVal ABC As Nullable(Of Integer), ByVal DEF As String) _
           As IEnumerable(Of MyProjectStoredProcResults) Implements MyFunction.GetThatDataResult

    Dim y = (From p In dc.uspReportMyFunction(ABC, DEF)
        Select p).ToList

    Return y

End Function

我相信linqtosql会自动处理可空的翻译。

答案 1 :(得分:0)

你可以使整数可以为空,只有在它有值时才传递它:

请注意ABC如何以Nullable类型传递:

Public Function RunStoredProce(ByVal ABC As Integer?, ByVal DEF As String) As IEnumerable(Of MyProjectStoredProcResults) Implements MyFunction.GetThatDataResult
    Return (From p In dc.uspReportMyFunction(ABC, DEF) Select p).ToList
End Function

我不知道你是如何设置系统的,但假设uspReportMyFunction是调用存储过程的函数,你可能希望在它为空时排除ABC

If ABC.HasValue Then
    'Add ABC as a parameter
End If

'Add DEF as a parameter

'Execute the stored procedure

修改

假设这是执行存储过程的数据函数

public void SaveSomething(int jobID, int? postNumber)
{
    using (var data = new DataAccess(this.ConnectionString))
    {
        data.ProcedureName = "SaveSomething";

        data.AddParm("@JobID", SqlDbType.Int, JobID);
        if (postNumber.HasValue)
            data.AddParm("@PostNumber", SqlDbType.Int, PostNumber.GetValueOrDefault());

        return data.ExecNonQuery();
    }
}

从前端,你可以这样调用这个函数:

int jobID = 1;
int? postNumber = null;

GetWorkOrderDetailAdjustment(jobID, postNumber); //postNumber == null