2013-06-12 263 views
-1

我工作在C#,WPF与MVVM和SQL Server存储过程2008年存储过程,空参数

我发送值的功能时,一个问题是执行存储过程,其中,我从一张表格中获得,在表格中可以留下一些空的空间。但是这些是一个类的对象,那么访问它们不应该是null。我离开代码。

 public IEnumerable<Model.AsuntoModel> GetBusqueda(Model.PrioridadModel prioridad, Model.StatusAsuntoModel statusasunto, Model.DestinatarioModel destinatario, Model.SignatarioModel signatario, DateTime rangofecha, DateTime referenciadocumento) 
    { 
     ObservableCollection<Model.AsuntoModel> Busqueda = new ObservableCollection<Model.AsuntoModel>(); 
     using (var entity = new GestorDocumentEntities()) 
     { 

      try 
      { 
       entity.GetAsuntos(prioridad.IdPrioridad, statusasunto.IdStatusAsunto, destinatario.IdDestinatario, signatario.IdSignatario, referenciadocumento).ToList().ForEach(p => 
       { 
        Busqueda.Add(new Model.AsuntoModel() 
        { 
         IdAsunto = p.IdAsunto, 
         FechaCreacion = (DateTime)p.FechaCreacion, 
         FechaRecibido = (DateTime)p.FechaRecibido, 
         FechaDocumento = (DateTime)p.FechaDocumento, 
         Titulo = p.Titulo, 
         Descripcion = p.Descripcion, 
         Alcance = p.Alcance, 
         IdUbicacion = (long)p.IdUbicacion, 
         Ubicacion = new Model.UbicacionModel() 
         { 
          UbicacionName = p.CAT_UBICACION.UbicacionName 
         }, 
         IdInstruccion = (long)p.IdInstruccion, 
         Instruccion = new Model.InstruccionModel() 
         { 
          InstruccionName = p.CAT_INSTRUCCION.InstruccionName 
         }, 
         IdPrioridad = (long)p.IdPrioridad, 
         Prioridad = new Model.PrioridadModel() 
         { 
          PrioridadName = p.CAT_PRIORIDAD.PrioridadName 
         }, 
         IdStatusAsunto = p.IdStatusAsunto, 
         StatusAsunto = new Model.StatusAsuntoModel() 
         { 
          StatusName = p.CAT_STATUS_ASUNTO.StatusName 
         }, 
         FechaVencimiento = p.FechaVencimiento, 
         Folio = p.Folio 
        }); 
       }); 

      } 
      catch (Exception) 
      { 
       ; 
      } 
     } 
     return Busqueda; 
    } 

回答

0

你的SP可以被编码治疗NULL PARMS为已任。

select * 
from table 
where (table.col = @COL or @COL IS NULL) 
and (table.col2 = @COL2 or @COL2 IS NULL) 
2

这是许多解决方案之一。

您只需让SQL参数为空,方法是给它们一个默认值。

E.g:

@ParamA = null 
@ParamB = null 
etc.. 

然后你管理这个SQL一边看你已经收到了多少。
这样,如果您不提供任何参数,您的查询将具有一个值(在这种情况下为null)。然后你可以测试它:

IF @ParamA IS NULL 
BEGIN 
-- You didn't give that param. 
END 
ELSE 
-- You gave a param