Click here to Skip to main content
11,496,146 members (519 online)
Click here to Skip to main content

Tagged as

Parameters - SqlCommand vs. OledbCommand and OdbcCommand

, 26 Jul 2011 CPOL 28.9K 3
A quick tip on using OledbCommand and OdbcCommand
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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.
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.
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)

Share

About the Author

walterhevedeich
Software Developer (Senior)
Philippines Philippines
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionDisagree Pin
Maciej Los6-Dec-14 5:38
protectorMaciej Los6-Dec-14 5:38 
GeneralMy vote of 5 Pin
ajit3026-Nov-14 0:18
memberajit3026-Nov-14 0:18 
GeneralMy vote of 5 Pin
Herbisaurus21-Aug-13 22:56
memberHerbisaurus21-Aug-13 22:56 
NewsSome results from my research [modified] Pin
randomusic22-Nov-12 12:53
memberrandomusic22-Nov-12 12:53 
GeneralRe: Some results from my research Pin
walterhevedeich21-Aug-13 23:56
professionalwalterhevedeich21-Aug-13 23:56 
GeneralRe: Some results from my research Pin
randomusic23-Aug-13 1:43
memberrandomusic23-Aug-13 1:43 
GeneralReason for my vote of 5 Its an important tip. Few months ago... Pin
thatraja8-Oct-11 21:08
mvpthatraja8-Oct-11 21:08 
GeneralRe: Thanks Raja. Error messages can really be confusing sometime... Pin
walterhevedeich10-Oct-11 4:16
memberwalterhevedeich10-Oct-11 4:16 
GeneralReason for my vote of 3 good work Pin
weekmo1-Aug-11 21:09
memberweekmo1-Aug-11 21:09 
GeneralReason for my vote of 4 Quick and to the point. Good contrib... Pin
BrianBissell1-Aug-11 17:34
memberBrianBissell1-Aug-11 17:34 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 26 Jul 2011
Article Copyright 2011 by walterhevedeich
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid