指定演员表无效。 Linq中的存储过程

时间:2012-05-16 11:51:15

标签: .net linq sql-server-2008 c#-4.0

Hello下面是我在C#中的代码

var a = 
from c in HRM_dc.uspCalculateRiskMatrix(CashPrice, InputAPH, Bushels, PercentageCover, BasicEstimate, CallStrike, CallBu, CallPremium,PutStrike, PutBu, PutPremium, TotalAcres, AvgPrice, PerAcreProductionCost, SpringPrice)

                            select new
                            {
                                bushels = c.Bushel,
                                row1 = c._0_00.Value

                            };
                    gvRisk.DataSource = a;
                    gvRisk.DataBind();

请帮我解释为什么会出现此错误

存储过程

ALTER PROC [dbo].[uspCalculateRiskMatrix]


@CashPrice MONEY, @InputAPH INT, @Bushels INT, @PercentageCover FLOAT, @BasicEstimate FLOAT, 


@CallStrike FLOAT, @CallBu INT,


@CallPremium FLOAT, @PutStrike FLOAT, @PutBu INT, @PutPremium FLOAT, @TotalAcres FLOAT, @AvgPrice

 MONEY, @PerAcreProductionCost MONEY,

@SpringPrice MONEY

AS

BEGIN

SET NOCOUNT ON

DECLARE @BushelInterval INT, @BushelRange INT, @BushelValueCounter INT


DECLARE @PriceInterval FLOAT, @PriceRange INT, @PriceValueCounter FLOAT


DECLARE @CurrRow INT


IF 1 = 2


BEGIN


SELECT NULL AS Bushel, NULL AS [0.00], NULL AS [1.00], NULL AS [2.00], NULL AS [3.00], NULL AS [4.00]


,NULL AS [5.00], NULL AS [6.00], NULL AS [7.00], NULL AS [8.00], NULL AS [9.00], NULL AS [10.00]


END

CREATE TABLE #tblTemp (Bushel VARCHAR(30), [0.00] FLOAT, [1.00] FLOAT, [2.00] FLOAT, [3.00] FLOAT, 

[4.00] FLOAT

, [5.00] FLOAT, [6.00] FLOAT, [7.00] FLOAT, [8.00] FLOAT, [9.00] FLOAT, [10.00] FLOAT)

SET @BushelInterval = 10

SET @BushelRange = 5

SET @BushelValueCounter = 0

SET @PriceInterval = 0.15

SET @PriceRange = 5

SET @PriceValueCounter = 0

SET @CurrRow = 1

DECLARE @tblBushelRow TABLE (ID INT IDENTITY(1,1), BushelValue INT)

DECLARE @tblPriceCol TABLE (ID INT IDENTITY(1,1), CashPrice MONEY)

CREATE TABLE #tblMatrix (ID INT IDENTITY(1,1), Bushel VARCHAR(30), CashPrice MONEY, CalValue FLOAT)

INSERT INTO @tblBushelRow (BushelValue) VALUES(@Bushels)

WHILE @CurrRow <= @BushelRange
BEGIN


SET @BushelValueCounter = @BushelValueCounter + @BushelInterval

INSERT INTO @tblBushelRow (BushelValue) VALUES(@Bushels + @BushelValueCounter)

INSERT INTO @tblBushelRow (BushelValue) VALUES(@Bushels - @BushelValueCounter)

SET @CurrRow = @CurrRow + 1

END

SET @CurrRow = 1

INSERT INTO @tblPriceCol (CashPrice) VALUES(@CashPrice)

WHILE @CurrRow <= @PriceRange

BEGIN

SET @PriceValueCounter = @PriceValueCounter + @PriceInterval

INSERT INTO @tblPriceCol (CashPrice) VALUES(@CashPrice + @PriceValueCounter)

INSERT INTO @tblPriceCol (CashPrice) VALUES(@CashPrice - @PriceValueCounter)

SET @CurrRow = @CurrRow + 1
END

INSERT INTO #tblMatrix (Bushel, CashPrice)

SELECT T1.BushelValue, T2.CashPrice FROM @tblBushelRow T1 CROSS JOIN @tblPriceCol T2 ORDER BY 

T1.BushelValue DESC, T2.CashPrice ASC

--SELECT * FROM #tblMatrix

UPDATE #tblMatrix SET CalValue = dbo.udfGetCornMatrixValue(CashPrice, @InputAPH, Bushel, 

@PercentageCover, @BasicEstimate, @CallStrike, @CallBu, @CallPremium,

@PutStrike, @PutBu, @PutPremium, @TotalAcres, @AvgPrice, @PerAcreProductionCost, @SpringPrice)


DECLARE @cols NVARCHAR(MAX), @query NVARCHAR(4000)

SELECT @cols = (SELECT STUFF((SELECT DISTINCT ', '+QUOTENAME(CashPrice) FROM #tblMatrix FOR XML PATH

('')),1,1,''))

--print @cols

SET @query = N'SELECT DISTINCT ''Bushel'', '+@cols +'FROM(SELECT Bushel, CashPrice FROM #tblMatrix) p

PIVOT(SUM(CashPrice) FOR CashPrice IN( '+@cols +' )) AS pvt;'


INSERT INTO #tblTemp(Bushel, [0.00], [1.00], [2.00], [3.00], [4.00], [5.00], [6.00], [7.00], [8.00], [9.00], [10.00])

EXECUTE(@query)

SET @query = N'SELECT Bushel, '+@cols +'FROM(SELECT CAST(Bushel AS VARCHAR) AS Bushel, CashPrice, CalValue FROM #tblMatrix) p
PIVOT(SUM(CalValue) FOR CashPrice IN( '+@cols +' )) AS pvt;'

INSERT INTO #tblTemp(Bushel, [0.00], [1.00], [2.00], [3.00], [4.00], [5.00], [6.00], [7.00], [8.00], [9.00], [10.00])

EXECUTE(@query)

SELECT Bushel, [0.00], [1.00], [2.00], [3.00], [4.00], [5.00], [6.00], [7.00], [8.00], [9.00], [10.00] FROM #tblTemp


DROP TABLE #tblMatrix, #tblTemp

1 个答案:

答案 0 :(得分:0)

这里只有太多的代码 - 这个SP不是一个很好的阅读......

但是基于您为响应SLaks的注释而给出的堆栈跟踪,无论您尝试读取为整数的存储过程返回的列中的值实际上都不是整数。

我会猜测它是[0.00]列 - 在这种情况下,当您访问c._0_00.Value时发生错误。从你正在做.Value的事实判断,我的猜测是它被输入为int??如果它为null(基础数据读取器中为DBNull),那么我认为你不会得到强制转换异常,但我可能错了。因此,我认为实际值是非空字符串或其他值。

如果它是空的那就是问题,那么linq语句中的基本where子句就可以了。在select new行之前坚持这一点:

where c._0_00 != null

事实上你应该把它放在一边,除非你能保证在那一列中不会出现NULL

但如果它是返回值的基础数据类型,那么您应该考虑修改SP以确保始终在这些列中返回一个int。

最好的办法是在Management Studio中使用相同的参数运行SP并查看结果集。从中可以看出这一点。