简体   繁体   English

参数化的存储过程返回“指定的参数过多”

[英]Parameterized stored procedure returning “too many arguments specified”

I'm writing an ASP.NET(C#) application in Visual Studio 2008 and connecting to SQLExpress 2005. 我在Visual Studio 2008中编写一个ASP.NET(C#)应用程序,并连接到SQLExpress 2005。

While trying to update a FormView control bound to an SqlDataSource by using a parameterized stored procedure, I constantly get an error screen saying "too many arguments specified". 当尝试通过使用参数化的存储过程更新绑定到SqlDataSource的FormView控件时,我不断收到错误屏幕,提示“指定了太多参数”。

I have tried clearing the list and adding all the parameters manually before calling the DataSource.Update() method. 我尝试清除列表并手动添加所有参数,然后再调用DataSource.Update()方法。 I have tested with a breakpoint and immediately before the Update method fires, the UpdateParameters collection holds the 8 arguments I have specified in my stored procedure so I know my collection conforms to what I asked for. 我已经使用断点进行了测试,并且在Update方法触发之前,UpdateParameters集合保存了我在存储过程中指定的8个参数,因此我知道我的集合符合我的要求。

Passing in update commands of type="text" that contain an EXEC statement will work but I need it to work by calling the procedure itself. 传递包含EXEC语句的type =“ text”更新命令可以正常工作,但我需要通过调用过程本身来使其工作。

Has anyone else run into these "extra arguments" or am I playing EPR and chasing imaginary variables? 还有其他人遇到这些“额外参数”吗?还是我在玩EPR并追逐虚构变量?

CREATE PROC spUpdateUserProfile
 @UserNameVar nvarchar(256),
 @DisplayNameVar varchar(30),
 @FNameVar varchar(20),
 @LNameVar varchar(20),
 @EmailVar varchar(30)=NULL,
 @LocationVar varchar(100)=NULL,
 @BirthdateVar smalldatetime=NULL,
 @BiographyVar varchar(2000)=NULL

AS

UPDATE UserProfile
SET UserDisplayName = @DisplayNameVar,
 UserFName = @FNameVar,
 UserLName = @LNameVar,
 UserSharedEmail = @EmailVar,
 UserLocation = @LocationVar,
 UserDOB = @BirthdateVar,
 UserBiography = @BiographyVar
WHERE UserProfile.UserID = 
(SELECT UserProfile.UserID FROM UserProfile
JOIN aspnet_Users ON UserProfile.UserID = aspnet_Users.UserId
WHERE aspnet_Users.UserName = @UserNameVar)

Just a shot in the dark until we can see some code like James asked, but are you settings the DataKeyNames attribute? 只是在黑暗中一枪,直到我们看到像James询问的代码为止,但是您是否设置了DataKeyNames属性? When I was getting started with FormView and GridView I manually added the primary key value using a hidden field and had the DataKeyNames attribute sent and I think that caused the value to be sent to the stored procedure twice instead of once. 当我开始使用FormView和GridView时,我使用隐藏字段手动添加了主键值,并发送了DataKeyNames属性,我认为这导致该值两次发送到存储过程,而不是一次。

Just a guess 只是一个猜测

EDIT: Have you tried 编辑:您是否尝试过

    UPDATE UserProfile
    SET UserDisplayName = @DisplayNameVar,
      UserFName = @FNameVar,
      UserLName = @LNameVar,
      UserSharedEmail = @EmailVar,
      UserLocation = @LocationVar,
      UserDOB = @BirthdateVar,
      UserBiography = @BiographyVar
    WHERE UserProfile.UserID = aspnet_Users.UserId
      AND aspnet_Users.UserName = @UserNameVar

change line: WHERE UserProfile.UserID = 更改行:WHERE UserProfile.UserID =
for: WHERE UserProfile.UserID IN 用于:WHERE UserProfile.UserID IN

I've just encountered this as well but I have managed to sort it. 我也刚刚遇到了这个问题,但我设法对其进行了排序。

My update was from a grid. 我的更新来自网格。
My grid was populated from another stored procedure. 我的网格是从另一个存储过程填充的。 In that Select stored procedure I changed the field names to more user friendly ones eg 在“选择存储过程”中,我将字段名称更改为更加用户友好的名称,例如

select AU.UserName [Member],

Later, when I checked, the Update command was passing all the parameters required by the update stored procedure and extra ones corrsponding to the re-named fields. 后来,当我检查时,Update命令传递了update存储过程所需的所有参数以及与重命名字段相对应的其他参数。

I removed the re-naming from the Select procedure and updated the SqlDataSource. 我从Select过程中删除了重命名并更新了SqlDataSource。 Now only the correct number of fields get passed. 现在,仅传递正确数量的字段。

Then I just renamed the HeaderText tag of the GridBoundColumn.eg 然后,我刚刚重命名了GridBoundColumn.eg的HeaderText标签。

HeaderText="Member"

I am using a RadGrid rather than the standard GridView but it seems to work there as well. 我使用的是RadGrid,而不是标准的GridView,但它似乎也可以正常工作。

I checked the number of passed parameters using: 我使用以下命令检查了传递参数的数量:

protected void  SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    for (int x = 0; x <= e.Command.Parameters.Count - 1;x++ )
    {
        string Type = e.Command.Parameters[x].GetType().ToString();
        string Value = e.Command.Parameters[x].ToString();
    }
}

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM