0

你好下面是我在 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
4

1 回答 1

0

这里的代码实在是太多了——SP 不是一本好书……

但是根据您为响应 SLaks 的评论而给出的堆栈跟踪,您尝试将存储过程返回的列中的任何值都不是整数。

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

如果问题是空值,那么wherelinq 语句上的基本子句就可以了。将其粘贴select new在行前:

where c._0_00 != null

事实上,你应该把它放在任何地方,除非你能保证 noNULL永远不会出现在该列中。

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

最好的办法是在 Management Studio 中使用相同的参数运行 SP 并查看结果集。从中会很明显。

于 2012-05-16T12:06:07.630 回答