Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 例外情况;过程或函数需要未提供的参数;即使在提供参数后也会抛出_C#_.net_Sql Server_Stored Procedures_Sql Server 2012 - Fatal编程技术网

C# 例外情况;过程或函数需要未提供的参数;即使在提供参数后也会抛出

C# 例外情况;过程或函数需要未提供的参数;即使在提供参数后也会抛出,c#,.net,sql-server,stored-procedures,sql-server-2012,C#,.net,Sql Server,Stored Procedures,Sql Server 2012,我正在尝试使用存储过程在数据库中插入一些条目,该存储过程由C#方法调用,如下所示: public int addProfile(UserProfile NewUserPro) { DataSet ds = ExecuteStoredProcedure("AddNewPro", new SqlParameter[]{ new SqlParameter("@UserID", NewUserPro.UsrId), new SqlP

我正在尝试使用存储过程在数据库中插入一些条目,该存储过程由C#方法调用,如下所示:

public int addProfile(UserProfile NewUserPro)
{
    DataSet ds = ExecuteStoredProcedure("AddNewPro", new SqlParameter[]{
                new SqlParameter("@UserID", NewUserPro.UsrId),
                new SqlParameter("@Type", NewUserPro.Type),
                new SqlParameter("@Phone", NewUserPro.Phone),
                new SqlParameter("@Name", NewUserPro.Name),
                new SqlParameter("@FatherName", NewUserPro.FatherName),
                new SqlParameter("@GroupID", NewUserPro.GroupID),
                new SqlParameter("@IsPublic", 0)});
            return int.Parse(ds.Tables[0].Rows[0][0].ToString());
}
ExecuteStoredProcedure()
是我创建的一个C#方法。它执行指定的存储过程,并以
数据集的形式返回输出。这是该函数的定义:

public DataSet ExecuteStoredProcedure(string ProcedureName, SqlParameter[] Parameters)
{
    try
    {
         using (SqlCommand com = new SqlCommand("[dbo]." + ProcedureName))
         {
              com.CommandType = CommandType.StoredProcedure;
              foreach (SqlParameter sp in Parameters)
                   com.Parameters.Add(sp);
              return SelectDataSet(com);
         }
    }
    catch (Exception e)
    {
        throw e;
    }
}   
正如您在
addProfile()
方法中看到的,我已经提供了
@IsPublic
参数,但它仍然引发以下异常:

过程或函数“AddNewPro”需要参数“@IsPublic”, 没有提供

@IsPublic
参数是一个
int
参数,用于将值保存到
[IsPublic]
字段,该字段是表中允许
null
int
字段

更新1

我正在包括存储过程的输入参数。我想那就足够了?如果您愿意,我可以包括完整的存储过程,但这是一个很长的代码,由于一些保密的原因,我必须削减一些行

ALTER PROCEDURE [dbo].[AddNewPro]
    @UserID int,
    @Name NVARCHAR(MAX),
    @Type int,
    @Phone VARCHAR(MAX),
    @GroupID int,
    @FatherName VARCHAR(MAX),
    @IsPublic int
AS
更新2


好的,我找到了解决方案的一点提示。当我将断点放在
ExecuteStoreProcedure
的第一行时,我发现它将
@IsPublic
的值设置为null。但我在添加参数时提供了值。为什么会这样

sqlparameter将零作为对象读取


注意使用零,阅读这篇文章

必须先将零转换为整数类型。 您的代码应该如下所示

new SqlParameter("@IsPublic", Convert.ToInt32(0));

请将您的
AddNewPro
存储过程包含在此处。与您的问题相关,我建议您仔细考虑一下
ExecuteStoredProcedure
中的
try catch
。it服务器不仅没有真正的用途(它捕获并抛出所有异常),而且没有正确地重新抛出它们。您最好删除它。@maku我包括了输入参数声明部分。希望它足够了?小心使用零,阅读这篇文章这应该是新的SqlParameter(@IsPublic),Convert.ToInt32(0))sqlparameter将零读取为objectGolf clap!我猜它抓取了错误的构造函数重载,但你先到了那里。@EdPlunkett你是什么意思?我在恭维你之前回答正确。没问题!我猜可能是这样。