简体   繁体   中英

SQL Server stored procedure call

I have a stored procedure defined in the database and I am trying to call it from the C# server.

In my repository I have a generic method that looks like this:

private IList<T> ExecuteReturningProcedure<T>(string procedureName, params ValueTuple<string, object>[] parameters)
        {
            List<T> objList = null;

            ExecuteCommand(procedureName, parameters, CommandType.StoredProcedure, (cmd) =>
            {
                using (var reader = cmd.ExecuteReader())
                {
                    objList = MapToList<T>(reader).ToList();
                }
            });

            return objList;
        }

and I made the specific method for my call that looks like this:

public List<dynamic> GetDataSubmissionEntries(Guid dataSubmissionId)
        {
            var parameters = Array.Empty<ValueTuple<string, object>>(); 
            parameters.Append(new ValueTuple<string,object>("@dataSubmissionId", dataSubmissionId));

            var entries = ExecuteReturningProcedure<dynamic>("[DataSubmission].[RetrieveDataSubmissionEntries]", parameters);

            return entries.ToList();
        }

The problem is that when the code is executed I get an exception with the message: Procedure or function 'RetrieveDataSubmissionEntries' expects parameter '@dataSubmissionId', which was not supplied.

Am I contructing the array of parameters the wrong way?

Try setting the parameters for the SP like this:

SqlCommand cmd_Sp = new SqlCommand("YourSPname", conn);
cmd_Sp.CommandType = CommandType.StoredProcedure;
cmd_Sp.Parameters.Add("@dataSubmissionId", SqlDbType.VarChar).Value = dataSubmissionId.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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