从前端

时间:2015-05-07 10:43:33

标签: asp.net sql-server

ALTER PROCEDURE [dbo].[spUpdateQuizScore]
-- Add the parameters for the stored procedure here
@Score INT=0,
@CorrectAnswer INT=0,
@WrongAnswer INT=0,
@TimeSpent INT=0,
@QuizId INT=0,
@UserId INT=0,
@UserName NVARCHAR(200)="No User",
@GroupId INT=0,
@GroupName NVARCHAR(200)="NoGroup"
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO QuizScores(Score, CorrectAnswers, WrongAnswers, TimeSpent, QuizId, UserId,
       UserName, GroupId, GroupName) 
    VALUES(@Score, @CorrectAnswer, @WrongAnswer, @TimeSpent, @QuizId, @UserId,
       @UserName, @GroupId, @GroupName)
            END 

这是我的存储过程,我将数据插入测验分数

 public void UpdateUserQuizScore(int correctAnswer, int wrongAnswer, int score, int quizId, int userId,
            string userName, int groupId, string GroupName,int sid)
        {

            var dbCon = new DBConnection("Quiz");
            dbCon.AddParameter("@Score", score);
            dbCon.AddParameter("@CorrectAnswer", correctAnswer);
            dbCon.AddParameter("@WrongAnswer", wrongAnswer);
            dbCon.AddParameter("TimeSpent", 1);
            dbCon.AddParameter("@QuizId", quizId);
            dbCon.AddParameter("@SessionId", sid);
            if (userId != null && groupId == null)
            {
                dbCon.AddParameter("@UserId", userId);
                dbCon.AddParameter("@UserName", userName);
            }
            if (groupId != null && userId == null)
            {
                dbCon.AddParameter("@GroupId", userId);
                dbCon.AddParameter("@GroupName", userName);
            }
            if (groupId != null && userId != null)
            {
                dbCon.AddParameter("@UserId", userId);
                dbCon.AddParameter("@GroupId", groupId);
                dbCon.AddParameter("@UserName", userName);
                dbCon.AddParameter("@Groupname", GroupName);
            }
            dbCon.Execute_NonQuery("spUpdateQuizScore", null);
        }

这是执行时我的服务器端代码我收到此错误 - 过程或函数spUpdateQuizScore指定了太多参数。

2 个答案:

答案 0 :(得分:3)

错误信息非常清楚。从前端传递的输入参数数量大于存储过程中预期的参数数量。

您的存储过程只需要这些输入参数:

@Score INT=0,
@CorrectAnswer INT=0,
@WrongAnswer INT=0,
@TimeSpent INT=0,
@QuizId INT=0,
@UserId INT=0,
@UserName NVARCHAR(200)="No User",
@GroupId INT=0,
@GroupName NVARCHAR(200)="NoGroup"

例如,您正在传递:

dbCon.AddParameter("@SessionId", sid);
从前端

并且在您的过程中没有将参数定义为@SessionId。

<强>解决方案:

从解决方案的角度来看,您需要更改存储过程并添加您在过程中传递的缺失参数。像这样:

ALTER PROCEDURE [dbo].[spUpdateQuizScore]
 -- Add the parameters for the stored procedure here
@Score INT=0,
@CorrectAnswer INT=0,
@WrongAnswer INT=0,
@TimeSpent INT=0,
@QuizId INT=0,
@UserId INT=0,
@UserName NVARCHAR(200)="No User",
@GroupId INT=0,
@GroupName NVARCHAR(200)="NoGroup",
@SessionId INT=0

答案 1 :(得分:1)

更新您的存储过程。您错过了此处的SessionId

ALTER PROCEDURE [dbo].[spUpdateQuizScore]
 -- Add the parameters for the stored procedure here
@Score INT=0,
@CorrectAnswer INT=0,
@WrongAnswer INT=0,
@TimeSpent INT=0,
@QuizId INT=0,
@UserId INT=0,
@UserName NVARCHAR(200)="No User",
@GroupId INT=0,
@GroupName NVARCHAR(200)="NoGroup",
@SessionId int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
INSERT INTO QuizScores(Score,CorrectAnswers,WrongAnswers,TimeSpent,QuizId,UserId,UserName,GroupId,GroupName,SessionId) VALUES(@Score,@CorrectAnswer,@WrongAnswer,@TimeSpent,@QuizId,@UserId,@UserName,@GroupId,@GroupName,@SessionId)
END