2

Is this really true that order of sql parameters in sql stored procedure must match the order of sql parameters used in SqlSquery:

 var Parameter = new List<SqlParameter>();
 Parameter.Add(new SqlParameter("@ParamOne", 1));
 Parameter.Add(new SqlParameter("@ParamTwo", 2));
 Parameter.Add(new SqlParameter("@ParamThree", 3));

context.Database.SqlQuery<myEntityType>("exec sp_Stored_Procedure @ParamOne, @ParamTwo, @ParamThree", Parameter.ToArray()).ToList<myEntityType>();

And in case sql developer decides to change stored procedure in the following way

ALTER PROCEDURE [dbo].[sp_Stored_Procedure]
@ParamOne as int,
@ParamTwo as int,
@ParamThree as int, 

AS ......

the result set will be empty?

This could raise many issues and I am asking is there something I am missing in this scenario?

2

2 Answers 2

1

I was oblivious to the situation at first!

Something like this should resolve your issue:

 var Parameter = new List<SqlParameter>();
 Parameter.Add(new SqlParameter("@p1", 1));
 Parameter.Add(new SqlParameter("@p2", 2));
 Parameter.Add(new SqlParameter("@p3", 3));

context.Database.SqlQuery<myEntityType>("exec sp_Stored_Procedure @ParamOne = @p1, @ParamTwo = @p2, @ParamThree = @p3", Parameter.ToArray()).ToList<myEntityType>();
Sign up to request clarification or add additional context in comments.

Comments

0

This could actually work BOTH ways.

If you have a query:

"exec sp_Stored_Procedure @p0, @p1, @p2"

Your parameters array will be assigned in order.

However if you have a query:

"exec sp_Stored_Procedure @username, @password"

Use an array of SqlParameter e.g. new SqlParameter("@password", userSuppliedPassword) then order is irrelevant.

4 Comments

There is no real difference between these two beside param name.
This article describes the variation in the parameters a little better: msdn.microsoft.com/en-us/library/gg696545(v=vs.113).aspx
I have changed my code so you can better understand the problem
Oh i see what you're suggesting. I'll update my answer with a workaround.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.