Click here to Skip to main content
15,891,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There have been instances where I've passed data to procedures as various parameters and even I've passed data to the procedure by creating a single XML parameter for whole data to send to procedure.

So I want to know which is a better approach of passing data to procedure among "passing various parameters differently" or "passing data as a single XML element"?;
Situations where passing data as XML provides advantage over passing parameters?

What I have tried:

Which is a better approach of passing data to procedure among "passing various parameters differently" or "passing data as a single XML element"?;
Situations where passing data as XML provides advantage over passing parameters?
Posted
Updated 25-Aug-17 6:59am
Comments
Thomas Nielsen - getCore 25-Aug-17 3:23am    
What kind of procedures? Do you men within C#, the so called methods of classes? or SQL stored procedures`? or RPC outside thread compartment like to another process or machine?
knackCoder 25-Aug-17 4:23am    
I'm referring to SQL stored procedures.

Quote:
I want to know which is a better approach of passing data to procedure

SQL? As parameters to avoid SQL Injection[^].
 
Share this answer
 
Id's say make an object to centralize data access (as such can more easily be tested and mocked out in unit tests) and in that one have your average parameter checking, escape sequence checking and then pass as parameter and you should bee sort of belt and suspenders and can optimize your data connections usage etc.

C#
using (var cn = GetConnection())
        {
            using(var cmd = new SqlCommand("spMyProcedure", cn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", invoiceNumber);
                cmd.Parameters.AddWithValue("@comment", FlareOnBadChars(comment));
                cmd.Parameters.AddWithValue("@note", FlareOnBadChars(note));
                var affect = (int)cmd.ExecuteScalar();

                if (affect != 1)
                    throw new DataConcernsException(string.Format("Expected to insert data but failed with data: '{0}','{1}','{2}'", invoiceNumber, comment, note));
            }
        } //Implicitly dispose already calls close on your connection


a nice addon is to check your string type parameters for escape sequence like the simplified one below ... you don't really want odd exceptions from parameter binding or sql ... much better to know exactly what's wrong if this thing occurs

private string FlareOnBadChars(string source)
       {
           if (!Regex.IsMatch(source, @"\A[^\\]+\Z"))  //TODO: add other things that you don't like before the ]
               throw new DataConcernsException("That's not a nice string");
           return source;
        }



As an alternative consider using a repository framework like entity framework, this will take care of parameter handling for you and you cannot avoid doing it right so to say ;)
 
Share this answer
 
Comments
Graeme_Grant 25-Aug-17 6:34am    
"can more easily be tested and mocked out in unit tests" - do you really think that he can do this considering the question?
Thomas Nielsen - getCore 28-Aug-17 2:40am    
Well like a Bulgarian programmer friend phrased it: Greatness waits for those who try, none can teach you. it's all inside, just climb!
Graeme_Grant 28-Aug-17 2:41am    
;)
The only correct answer is, "it depends". :)

If you're passing a single set of values to a stored procedure, then it's usually better to pass individual parameters. The only time you'd need a different solution would be if you needed to pass more than the maximum number of parameters allowed. But since that limit is 2100 per procedure, if you ever reach it, you urgently need to re-evaluate your database design!

If you're passing multiple values to a stored procedure, you have several options:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900