Click here to Skip to main content
15,891,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am learning how to use parameterized query using ADO on SQL Server CE.
My question is, after appending parameters to the parameters collection, how does the comnand object tell the position of each parameter in the query.

Let's say I have the following query:


INSERT INTO Customers (CustomerName,CustomerAddr,CustomerCity) values(? , ?, ?)

How does it tell the orde of the paramers since no information is given about this when parameters were created and when they are appendes.Or does it deduce it from the order in which the parameters are. added?If yes, must that order be observed in writing the quety. I.e. in the example query. above , must the fields name be ordered in the order in which the parameters were appended?
Posted

1 solution

It tells the order by having to specify the parameter values in that order.

However, a more robust solution would be to use named parameters, e.g.
C#
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Customers (CustomerName,CustomerAddr,CustomerCity) values(@name , @addr, @city)");
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 120).Value = "customer name";
cmd.Parameters.Add("@addr", SqlDbType.NVarChar, 120).Value = "customer address";
cmd.Parameters.Add("@city", SqlDbType.NVarChar, 120).Value = "customer city";
 
Share this answer
 
Comments
Gbenbam 21-Feb-14 5:32am    
Thanks.This is. so simple and straight forward.But this syntax. is C#'s isn't it?
Maarten Kools 21-Feb-14 5:43am    
Yes, I assumed you used C# but I now notice you've tagged it C++. Here is an example[^] in C++.
Gbenbam 2-Mar-14 9:48am    
Thanks your first solution is still very relevant.Whether C# or C++ , using named parameter is more rubust.By the way the C++ example you gave me link to doesnt use named parameters.That explains why your initial solution is still valid.

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