使用/ Parameters调用存储过程时出错

时间:2014-08-01 18:24:01

标签: c# entity-framework stored-procedures .net-4.5 sql-server-2012-express

当我尝试使用实体框架上下文调用我的存储过程时,我在运行时收到以下错误。 “参数化查询”(@Mac nvarchar(30),@ tagList [TagListType] READONLY)EXEC SaveMsg'需要参数'@Mac',这是未提供的。“

就行了

var result = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)databaseAPI).ObjectContext.ExecuteStoreCommand(command, new SqlParameter("@Mac",SqlDbType.NVarChar,30,reader.MAC),sqlParamTags);

以下是相关代码的其余部分。

DataTable dtTags = getDataTableTags(info.TagList.ToList());

//**call stored procedure

SqlParameter sqlParamTags = new SqlParameter("tagList", SqlDbType.Structured);
sqlParamTags.Value = dtTags;
sqlParamTags.TypeName = "TagListType";



string command = "EXEC " + "SaveMsgData" + " @Mac, @tagList;";

var result = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)databaseAPI).ObjectContext.ExecuteStoreCommand(command, new SqlParameter("@Mac",SqlDbType.NVarChar,100,reader.MAC),sqlParamTags);

这是我的存储过程

CREATE TYPE TagListType AS TABLE
(                     
    TagID nvarchar(100),                
    AntNum int
);
GO

CREATE PROCEDURE SaveMsgData 
@Mac nvarchar(30),
@tagList TagListType READONLY
AS

DECLARE @ReaderID int

DECLARE @AntList TABLE (AntennaID int, AntNum int, TargetNumOfTags int)

--SELECT @ReaderID = -1

SELECT @ReaderID = Id
    FROM dbo.Readers
    WHERE MAC = @Mac

IF (@ReaderID >= 0)
BEGIN
    INSERT INTO @AntList (AntennaID,Antnum,TargetNumOfTags) 
    SELECT id, AntennaNumber, numOfTags 
    FROM Antennae
    WHERE ID = @ReaderID

    DECLARE @count int
    DECLARE @len int                    
    SET @count = 0
    SET @len = 0

    WHILE @count <= @len
    BEGIN
        DECLARE @LocalAnt int
        DECLARE @LocalTarget int
        DECLARE @LocalAntID int
        DECLARE @LocalStatus int
        DECLARE @LocalTagsRead int

        --set local variables need in loop--
        SELECT @LocalAnt = Antnum, @LocalTarget = TargetNumOfTags, @LocalAntID = AntID FROM #AntList WHERE Antnum = @count

        --Decided where reading passed or failed--
        SELECT @LocalTagsRead = COUNT(*) FROM @tagList WHERE AntNum = @count

        IF @LocalTagsRead = @LocalTarget
            BEGIN
                SET @LocalStatus = 0
            END
        ELSE
            BEGIN
                SET @LocalStatus = 1
            END

        --insert Tags in to tag table--
        DECLARE @ReadingID int

        INSERT INTO Readings (Status,TargetNumOfTags,numOfTagsRead, TimeStamp, Antenna_Id, Reader_Id) VALUES ( @LocalStatus, @LocalTarget, @LocalTagsRead, CURRENT_TIMESTAMP, @LocalAntID, @ReaderID) 

        SELECT @ReadingID = SCOPE_IDENTITY()

        --insert Tags in to tag table--
        INSERT INTO Tags (TagID) SELECT TagID FROM @tagList WHERE AntNum = @LocalAnt

        --insert Tags and Readings in to TagReading table--
        INSERT INTO TagReadings (Tag_Id, Reading_ID) SELECT TagID,  @ReadingID FROM @tagList WHERE AntNum = @LocalAnt 

        SET @count = @count + 1
    END
END
GO

1 个答案:

答案 0 :(得分:1)

我认为SqlParameter没有带签名的重载(Name,SqlDbType,Size,Value)。您正在使用的重载是期望“SourceColumn”作为最终参数。因此,您正在创建参数,但未指定其值。

您需要将其更改为以下内容:

new SqlParameter("@Mac", reader.MAC);

参考:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx