IDataReader is the interface for all Datareader classes. SqlDataReader is a
SQL Server - specific implementation of IDataReader. OracleDataReader is the
Oracle - specific implementation. SQLiteDataReader --- etc.
Peter
As stated IDataReader is the interface that all "concrete" dataReaders
implement. As an example:
IDataReader dr;
SqlCommand cmdS = new SqlCommand();
OledbCommand cmdO = new OledbCommand();
dr = cmdS.ExecuteReader();
and
dr = cmdO.ExecuteReader()
are both valid as dr will take an instance of any type that implements
IDataReader. This is particularly useful when you are writing code that
will target different data sources. You can extend this further by using
the Interfaces for other data access objects.
for example:
IDbConnection cn; // interface that all connection objects implement
IDbCommand cmd; // interface that all command objects implement
IDataReader dr;
cn = new SqlConnection(ourConnectionString);
cmd = new SqlCommand(someSqlText);
dr = cmd.ExecuteReader();
Assuming that you are doing other things with your data access objects
you only need to change the lines that create the instances of your
obects to make the code work with another data source.
eg
cn = new OledbConnection(ourConnectionString);
cmd = new OledbCommand(someSqlText);
Take a look at the Object Factory design pattern which shows the true
power/reusability of this approach.
Well said and I'm complete now.. Thanks to Simon and Peter,,
Mahesh~
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.