Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Tip/Trick

Parameters - SqlCommand vs. OledbCommand and OdbcCommand

Rate me:
Please Sign up or sign in to vote.
4.90/5 (9 votes)
25 Jul 2011CPOL1 min read 78K   5   10
A quick tip on using OledbCommand and OdbcCommand
Using OledbCommand and OdbcCommand can be quite confusing, if you have used SqlCommand for some time. One difference is on the configuration of their parameters. For SqlCommand, it doesn't really care about the order of the parameters on your object so long as those parameters exist in the query. A snippet like the following would work. Notice how the parameters are positioned on the CommandText property and the order on how it is added on the object.
C#
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConnString"]);
SqlCommand comm = new SqlCommand();
SqlDataAdapter dAdpter = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
comm.Connection = conn;
comm.CommandText = "SELECT FirstName, LastName FROM tblMyChicks WHERE Age > @ageParam AND  LastName = @lNameParam";
comm.Parameters.AddWithValue("@lNameParam", "NoLastName");
comm.Parameters.AddWithValue("@ageParam", 18);
dAdpter.Fill(dt);

On the other hand, OleDbCommand and OdbcCommand does not support named parameters, and use ? placeholders instead. An example would be the following:
comm.CommandText = "SELECT FirstName, LastName FROM tblMyChicks WHERE Age > ? AND  LastName = ?";

One might believe that named parameters will work as writing a statement like SELECT FirstName, LastName FROM tblMyChicks WHERE Age > @ageParam AND LastName = @lNameParam is valid and won't throw any error(yes this is valid, and I believe its better to write them this way, for readability purposes). But in reality, it just treats it as a placeholder. As opposed to SqlCommand, it is important to take note of the order of how the parameters are added on the command object. The following code will result to a Data type mismatch in criteria expression. error.
C#
OleDbConnection conn = new OleDbConnection(ConfigurationManager.AppSettings["DBConnString"]);
OleDbCommand comm = new OleDbCommand();
OleDbDataAdapter dAdpter = new OleDbDataAdapter(comm);
DataTable dt = new DataTable();
comm.Connection = conn;
comm.CommandText = "SELECT FirstName, LastName FROM tblMyChicks WHERE Age > @ageParam AND  LastName = @lNameParam";
comm.Parameters.AddWithValue("@lNameParam", "NoLastName");
comm.Parameters.AddWithValue("@ageParam", 18);
dAdpter.Fill(dt);

Conclusion


Here are a few pointers that I hope I have explained:

1. SqlCommand uses named parameters, and the order of added parameter objects does not matter, so long as the parameter names exist on the query.
2. OleDbCommand and OdbCommand does not support named parameters and uses the ? placeholder instead, so the order of the parameters is important. However, you can give names to its parameters instead of using ?, for readability purposes.

License

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


Written By
Software Developer (Senior)
Philippines Philippines
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDisagree Pin
Maciej Los6-Dec-14 4:38
mveMaciej Los6-Dec-14 4:38 
GeneralMy vote of 5 Pin
ajit3025-Nov-14 23:18
professionalajit3025-Nov-14 23:18 
GeneralMy vote of 5 Pin
Herbisaurus21-Aug-13 21:56
Herbisaurus21-Aug-13 21:56 
NewsSome results from my research Pin
randomusic22-Nov-12 11:53
randomusic22-Nov-12 11:53 
GeneralRe: Some results from my research Pin
walterhevedeich21-Aug-13 22:56
professionalwalterhevedeich21-Aug-13 22:56 
Excellent explanation. My 5. Sorry I didn't bother looking at this post until today. Perhaps you may want to share (create another tip maybe) your routine on retrieving the queries from Access DB. And yeah, I agree with you that this contributes to the execution. This is probably the reason why some developers create a class on the code where they can put all their queries.
Signature construction in progress. Sorry for the inconvenience.

GeneralRe: Some results from my research Pin
randomusic23-Aug-13 0:43
randomusic23-Aug-13 0:43 
GeneralReason for my vote of 5 Its an important tip. Few months ago... Pin
thatraja8-Oct-11 20:08
professionalthatraja8-Oct-11 20:08 
GeneralRe: Thanks Raja. Error messages can really be confusing sometime... Pin
walterhevedeich10-Oct-11 3:16
professionalwalterhevedeich10-Oct-11 3:16 
GeneralReason for my vote of 3 good work Pin
Mohammed Abdulgadir1-Aug-11 20:09
Mohammed Abdulgadir1-Aug-11 20:09 
GeneralReason for my vote of 4 Quick and to the point. Good contrib... Pin
BrianBissell1-Aug-11 16:34
BrianBissell1-Aug-11 16:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.