1

我正在做一些项目并收到此错误消息:

*过程或函数 SP_XXX 指定了太多参数* 下面是我的 SQL 参数,我通过 ASP c# App 检查了许多站点,但一无所获:) 非常感谢您的帮助!

        cmd.Parameters.Add(new SqlParameter() { Value = user.CODE, ParameterName = "@CODE" });cmd.Parameters.Add(new SqlParameter() { Value = CODE, ParameterName = "@OLD_CODE" });
        cmd.Parameters.Add(new SqlParameter() { Value = COCODE, ParameterName = "@OLD_COCODE" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.COCODE, ParameterName = "@COCODE" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.MEMBERTYPE, ParameterName = "@MEMBERTYPE" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.ENNAME, ParameterName = "@ENNAME" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.ARNAME, ParameterName = "@ARNAME" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.NATIONALITY, ParameterName = "@NATIONALITY" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.GENDER, ParameterName = "@GENDER" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.SPECIAL, ParameterName = "@SPECIAL" });
        cmd.Parameters.Add(new SqlParameter() { Value = (object)user.BIRTHDATE ?? DBNull.Value, ParameterName = "@BIRTHDATE" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.Religion, ParameterName = "@RELIGION" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.QUALIFY, ParameterName = "@QUALIFY" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.MEMBERPOS, ParameterName = "@MEMBERPOS" });
        cmd.Parameters.Add(new SqlParameter() { Value = (object)user.JOINDATE ?? DBNull.Value, ParameterName = "@JOINDATE" });
        cmd.Parameters.Add(new SqlParameter() { Value = (object)user.EXPIRYDATE ?? DBNull.Value, ParameterName = "@EXPIRYDATE" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.POBOX, ParameterName = "@POBOX" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.WORKTEL, ParameterName = "@WORKTEL" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.HOMETEL, ParameterName = "@HOMETEL" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.MOBILE, ParameterName = "@MOBILE" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.FAX, ParameterName = "@FAX" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.EMAIL, ParameterName = "@EMAIL" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.WEBSITE, ParameterName = "@WEBSITE" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.ACCCODE, ParameterName = "@ACCCODE" });

        cmd.Parameters.Add(new SqlParameter() { Value = user.AMOUNT, ParameterName = "@AMOUNT" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.PAIDAMOUNT, ParameterName = "@PAIDAMOUNT" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.EMIRATE, ParameterName = "@EMIRATE" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.MILIT_NO, ParameterName = "@MILIT_NO" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.GRADE, ParameterName = "@GRADE" });

        //need job

        cmd.Parameters.Add(new SqlParameter() { Value = user.NEEDJOB, ParameterName = "@NEEDJOB" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.DEBIT, ParameterName = "@DEBIT" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.JOBNAME, ParameterName = "@JOBNAME" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.BNKNAME, ParameterName = "@BNKNAME" });
        cmd.Parameters.Add(new SqlParameter() { Value = (object)user.BNKAMOUNT ?? DBNull.Value, ParameterName = "@BNKAMOUNT" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.COMPAGNY, ParameterName = "@COMPAGNY" });
        cmd.Parameters.Add(new SqlParameter() { Value = (object)user.COMPAMOUNT ?? DBNull.Value, ParameterName = "@COMPAMOUNT" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.PRSN, ParameterName = "@PRSN" });
        cmd.Parameters.Add(new SqlParameter() { Value = (object)user.PRSNAMOUNT ?? DBNull.Value, ParameterName = "@PRSNAMOUNT" });
        if (user.PIC == null)
            cmd.Parameters.Add(new SqlParameter() { Value = DBNull.Value, ParameterName = "@PIC", SqlDbType = System.Data.SqlDbType.Image });
        else
            cmd.Parameters.Add(new SqlParameter() { Value = user.PIC, ParameterName = "@PIC" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.COMPANY, ParameterName = "@COMPANY" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.TYPEBLOOD, ParameterName = "@TYPEBLOOD" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.PASSPORTNUM, ParameterName = "@PASSPORTNUM" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.LEAVENUM, ParameterName = "@LEAVENUM" });
        cmd.Parameters.Add(new SqlParameter() { Value = user.POSBEFORE, ParameterName = "@POSBEFORE" });
 cmd.Parameters.Add(new SqlParameter() { Value = (object)user.LEAVEDATE ?? DBNull.Value, ParameterName = "@LEAVEDATE" });
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "SP_MSMEMBERS_UPDATE";
        cmd.Connection = con;
        string constr = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ToString();
        con.ConnectionString = constr;
        con.Open();
        int x = cmd.ExecuteNonQuery();

        con.Close();

    ALTER PROCEDURE [dbo].[SP_MSMEMBERS_UPDATE] 
       (
 @CODE VARCHAR(10),
 @OLD_CODE VARCHAR(10),
 @COCODE INTEGER,
 @OLD_COCODE INTEGER,
 @MEMBERTYPE INTEGER,
 @ENNAME VARCHAR(40),
 @ARNAME VARCHAR(40),
 @NATIONALITY INTEGER,
 @GENDER VARCHAR(1),
 @SPECIAL VARCHAR(1),
 @BIRTHDATE DATETIME,
 @RELIGION INTEGER,
 @QUALIFY INTEGER,
 @MEMBERPOS VARCHAR(40),
 @JOINDATE DATETIME,
 @EXPIRYDATE DATETIME,
 @POBOX VARCHAR(15),
 @WORKTEL VARCHAR(30),
 @HOMETEL VARCHAR(30),
 @MOBILE  VARCHAR(30),
 @FAX  VARCHAR(30),
 @EMAIL VARCHAR(40),
 @WEBSITE VARCHAR(80),
 @ACCCODE VARCHAR(20),
 @AMOUNT NUMERIC(15,3),
 @PAIDAMOUNT NUMERIC(15,3),
 @EMIRATE INTEGER,
 @MILIT_NO VARCHAR(40),
 @GRADE VARCHAR(20)
 --new add
 ,@NEEDJOB VARCHAR(1) 
 ,@DEBIT VARCHAR(1)
 ,@JOBNAME varchar(20)
 ,@BNKNAME varchar(25)
 ,@BNKAMOUNT NUMERIC(15,3)
 ,@COMPAGNY nvarchar(25)
 ,@COMPAMOUNT NUMERIC(15,3)
 ,@PRSN varchar(30)
 ,@PRSNAMOUNT NUMERIC(15,3)
 ,@PIC image
 ,@COMPANY varchar(25)
 ,@TYPEBLOOD varchar(20)
 ,@PASSPORTNUM varchar(20)
 ,@LEAVENUM varchar(10)
 ,@POSBEFORE varchar(15)
 ,@LEAVEDATE datetime
)
4

2 回答 2

3

抱歉发错帖子

其次,我运行 c# 代码如下:

public class MemberD
{
    public SqlCommand cmd = new SqlCommand();
    public SqlConnection con = new SqlConnection("ConStr");
    public SqlDataReader dr;

    public void UpdateMember(string CODE, int COCODE, Member user)
    {
        cmd.Parameters.Clear(); // Here's  the solution

        //Here I added my SQL parameters 
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "SP_MSMEMBERS_UPDATE";
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();

        con.Close();
    }
}

关键是我使用 SqlCommand 作为全局变量启动一次,所以当我使用两次更新方法或 GetByID 方法时,SQLCommand 变量也保留旧参数。因此,当您传递的参数超过 SP 需要 时,您会收到上述错误,所以我简单地添加了: cmd.Parameters.Clear(); 在每个方法的开头。

现在它工作正常。

于 2012-07-07T12:55:11.320 回答
0

该错误通常表示具有比您的 SP 定义的更多的参数。

由于您没有提供足够的代码来确定地告诉您(提供更多的 cmd 代码块和至少 SP 的定义),我认为是这种情况。尤其是在提供了这么多参数的情况下。

我确实读到这也可能是处理 SqlDatasources 时引起的潜在问题。但是,由于您提供的代码有限,我只是指出这是一种可能性。

于 2012-07-06T18:05:55.780 回答