开发者

Why does my stored procedure always think there is a value?

开发者 https://www.devze.com 2023-03-16 11:34 出处:网络
I have an ASP.NET 2010 app hitting a SQL 2005 db. I am atte开发者_如何学Cmpting to build a stored proc dynamically, depending on the values selected on a search screen. I have done this many times in

I have an ASP.NET 2010 app hitting a SQL 2005 db. I am atte开发者_如何学Cmpting to build a stored proc dynamically, depending on the values selected on a search screen. I have done this many times in previosu versions of .net & sql with no problem. However, now, my IF statement always always acts as though no data was passed in. I have debugged the app and am sure that data is being set. I should note that when I run the procedure directly, with or without data, the correct data is returned.

Here is the rather simple stored proc..

ALTER PROCEDURE get_cases_by_search_criteria
    @vin as varchar(30) = null
AS
declare @sqlstr varchar(1000)

set @sqlstr = 'SELECT 
[Case].CaseID, 
[Case].VIN, 
[Case].Make, 
[Case].Model, 
[Case].VehicleYear, 
if  @vin is not null and @vin <> ''
    set @sqlstr = @sqlstr + ' and ' + ('[Case].VIN = ''' + convert(varchar,@vin) + '''') 

exec(@sqlstr)

RETURN

And here is the code that calls the stored proc...

 Public Function GetCases(ByVal oSearchCriteria As SearchCriteria) As List(Of BE.Case)

    Dim lstCase As New List(Of BE.Case)
    Dim oCase As BE.Case
    Dim oProviderFactory As New ProviderFactory
    Dim oConnection As DbConnection
    Dim oReader As System.Data.IDataReader
    Dim oFactory As DbProviderFactory
    Dim oCmd As DbCommand
    Dim param1 As System.Data.Common.DbParameter

    Try
        'call my class to get an instance of the DBProviderFactory class
        oFactory = oProviderFactory.GetFactory
        'call another class of mine. pass in the DBProviderFactory class which will create a non-provider-specific connection object
        oConnection = oProviderFactory.GetProviderConnection(oFactory)

        'non-specific create command
        oCmd = oConnection.CreateCommand

        'non-specific parameter
        If oSearchCriteria.VIN.Length = 0 Then
            param1 = oFactory.CreateParameter()
            param1.ParameterName = "@vin"
            param1.DbType = DbType.String
            param1.Value = DBNull.Value
            oCmd.Parameters.Add(param1)
        Else
            param1 = oFactory.CreateParameter()
            param1.ParameterName = "@vin"
            param1.DbType = DbType.String
            param1.Value = oSearchCriteria.VIN
            oCmd.Parameters.Add(param1)

        End If

        oCmd.CommandType = CommandType.StoredProcedure
        oCmd.CommandText = "get_cases_by_search_criteria"

        Using (oConnection)
            oConnection.Open()
            oReader = oCmd.ExecuteReader()

            While oReader.Read
                oCase = New BE.Case

                'Case
                If oReader("CaseID") IsNot System.DBNull.Value Then oCase.CaseID = oReader("CaseID")

                If oReader("Make") IsNot System.DBNull.Value Then oCase.Make = oReader("Make")
                If oReader("Model") IsNot System.DBNull.Value Then oCase.Model = oReader("Model")
                If oReader("VehicleYear") IsNot System.DBNull.Value Then oCase.VehicleYear = oReader("VehicleYear")
                If oReader("VIN") IsNot System.DBNull.Value Then oCase.VIN = oReader("VIN")

                lstCase.Add(oCase)
            End While

            oConnection.Close()
        End Using
    Catch ex As Exception
        Throw ex
    End Try
    Return lstCase
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消