ASP.NET MVC存储过程

时间:2010-04-13 13:46:30

标签: .net linq-to-sql

我正在使用ms sql存储过程来获取一组记录,当我将此存储过程添加到我的LinqToSql类并使用它我的存储库时,它显示它返回一个int值,但它应该返回一个集合的行。我的存储过程或其他一些东西是否有问题???

ALTER PROCEDURE [dbo].[GetDocumentsAdvancedSearch]
    -- Add the parameters for the stored procedure here
      @SDI  CHAR(10) = NULL
     ,@Client CHAR(4) = NULL
     ,@AccountNumber VARCHAR(20) = NULL
     ,@Address VARCHAR(300)  = NULL
     ,@StartDate DATETIME = NULL
     ,@EndDate DATETIME = NULL
     ,@Job INT = NULL
     ,@Invoice INT = NULL
     ,@Amount MONEY = NULL
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
   -- DECLARE
    DECLARE @Sql        NVARCHAR(4000)
    DEClARE @ParamList  NVARCHAR(4000)
    DECLARE @AssociaID VARCHAR(6)
    --DECLARE 
    SET @AssociaID = 000000
    SELECT  @Sql = 'SELECT 
                    DISTINCT ISNULL(Documents.DocumentID, NULL) 
                   ,Person.Name1
                   ,Person.Name2
                   ,Person.Street1
                   ,Person.Street2
                   ,Person.CityStateZip
                   ,ISNULL(Person.ReferenceID,NULL)
                   ,ISNULL(Person.AccountNumber,NULL)
                   ,ISNULL(Person.HasSetPreferences,0)
                   ,Documents.Job
                   ,Documents.SDI
                   ,Documents.Invoice
                   ,ISNULL(Documents.ShippedDate,NULL)
                   ,ISNULL(Documents.DocumentPages,NULL)
                   ,Documents.DocumentType
                   FROM
                   Person
                   LEFT OUTER JOIN Documents ON Person.PersonID = Documents.PersonID
                   LEFT OUTER JOIN DocumentType ON Documents.DocumentType = DocumentType.DocumentType
                   LEFT OUTER JOIN Addresses   ON Person.PersonID = Addresses.PersonID
                   WHERE '                   

   IF NOT(@SDI IS NULL)
   SELECT @Sql = @Sql + ' Documents.SDI IN ('+@sdi+')' 
   IF NOT(@Client IS NULL)
   SELECT @Sql = @Sql + ' OR (Person.AssociationID = ' + @AssociaID + ' AND Person.Client ='+ @Client+')'   
   IF NOT(@AccountNumber IS  NULL)
   SELECT @Sql = @Sql + ' AND Person.AccountNumber LIKE ' + @AccountNumber
   IF NOT(@Address IS  NULL)
   SELECT @Sql = @Sql + ' AND Person.Name1 LIKE' + @Address + 'AND Person.Name2 LIKE' + @Address + ' AND Person.Street1 LIKE' + @Address + ' AND Person.Street2 LIKE' + @Address + ' AND Person.CityStateZip LIKE' + @Address
   IF NOT(@StartDate IS  NULL)
   SELECT @Sql = @Sql + ' AND Documents.ShippedDate >=' +@StartDate
   IF NOT(@EndDate IS NULL)
   SELECT @Sql = @Sql + ' AND Documents.ShippedDate <=' +@EndDate
   IF NOT(@Job IS NULL)
   SELECT @Sql = @Sql + ' AND Documents.Job =' +@Job
   IF NOT(@Invoice IS NULL)
   SELECT @Sql = @Sql + ' AND Documents.Invoice =' +@Invoice
   IF NOT(@Amount IS NULL)
   SELECT @Sql = @Sql + ' AND Documents.Amount =' +@Amount
    -- Insert statements for procedure here
    --PRINT @Sql
    SELECT @ParamList = '@Psdi CHAR(10),@PClient CHAR(4),@PAccountNumber VARCHAR(20),@PAddress VARCHAR(300),@PStartDate DATETIME ,@PEndDate DATETIME,@PJob INT,@PInvoice INT,@PAmount Money '
    EXEC SP_EXECUTESQL @Sql,@ParamList,@Sdi,@Client,@AccountNumber,@Address,@StartDate,@EndDate,@Job,@Invoice,@Amount
    --PRINT @Sql

END



[Function(Name="dbo.GetDocumentsAdvancedSearch")]
        public int GetDocumentsAdvancedSearch([Parameter(Name="SDI", DbType="Char(10)")] string sDI, [Parameter(Name="Client", DbType="Char(4)")] string client, [Parameter(Name="AccountNumber", DbType="VarChar(20)")] string accountNumber, [Parameter(Name="Address", DbType="VarChar(300)")] string address, [Parameter(Name="StartDate", DbType="DateTime")] System.Nullable<System.DateTime> startDate, [Parameter(Name="EndDate", DbType="DateTime")] System.Nullable<System.DateTime> endDate, [Parameter(Name="Job", DbType="Int")] System.Nullable<int> job, [Parameter(Name="Invoice", DbType="Int")] System.Nullable<int> invoice, [Parameter(Name="Amount", DbType="Money")] System.Nullable<decimal> amount)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), sDI, client, accountNumber, address, startDate, endDate, job, invoice, amount);
            return ((int)(result.ReturnValue));
        }

3 个答案:

答案 0 :(得分:1)

Linq to SQL无法识别EXEC SP_EXECUTESQL - 您需要手动定义输出。

http://social.msdn.microsoft.com/forums/en-US/linqtosql/thread/d7220c7a-d194-4d92-96dd-a3def25e9be9/

答案 1 :(得分:0)

我的猜测是,由于SQL是动态的,LINQ无法准确确定程序将返回的内容。

最佳解决方法可能是手动编辑.dbml文件

ETA:如果您不确定要放入.dbml文件中的内容,另一个选项可能是“欺骗”它,通过将一些SQL放入一个实际上永远不会实际存在的块中来确定从您的sproc返回的内容在实践中执行:

IF ( FALSE )
BEGIN
  SELECT 
  DISTINCT ISNULL(Documents.DocumentID, NULL) 
  ,Person.Name1
  ,Person.Name2
  ,Person.Street1
  ,Person.Street2
  ,Person.CityStateZip
  ,ISNULL(Person.ReferenceID,NULL)
  ,ISNULL(Person.AccountNumber,NULL)
  ,ISNULL(Person.HasSetPreferences,0)
  ,Documents.Job
  ,Documents.SDI
  ,Documents.Invoice
  ,ISNULL(Documents.ShippedDate,NULL)
  ,ISNULL(Documents.DocumentPages,NULL)
  ,Documents.DocumentType
  FROM
   Person
   LEFT OUTER JOIN Documents ON Person.PersonID = Documents.PersonID
   LEFT OUTER JOIN DocumentType ON Documents.DocumentType = DocumentType.DocumentType
   LEFT OUTER JOIN Addresses   ON Person.PersonID = Addresses.PersonID
END

答案 2 :(得分:0)

在DataContext布局(DBML)上手动创建一个包含存储过程返回的正确属性的类。在其属性中更改存储过程的返回类型。