Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MIDL
using (IDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                // get data from the reader
            }
        }



MIDL
using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                // get data from the reader
            }
        }


Hi,

Can someone tell me the difference between these two pieces of code ? Why use IDataReader ?
Posted
Comments
parmar_punit 26-May-11 10:02am    
Kindly see my new solution...... :)

IDataReader is an InterFace while SqlDataReader is class.... you can see it in the Object browser if you have Visual Studio....
for more details visit...
Link1[^]
Link2[^]
 
Share this answer
 
IDataReader is the generic interface that all data readers implement (including SqlDataReader). There is not much difference in your codes except that using the base interfaces allow you to change the database provider easily without any code change (like switching over from SQL Server to Oracle).
 
Share this answer
 
SqlDataReader is For only Sql Server where as ,IdataReader is used for all the Relational databases.
 
Share this answer
 
Comments
Close Network 26-May-11 6:46am    
Then, I have a new question. Interface does not hold any definition. Interface can not be instantiated. How can this code be valid ?

IDataReader reader = cmd.ExecuteReader()

cmdExecuteReader returns an object with a value in memory. reader is interface. How can an object be assigned to an interface ? isnt interface just a contract with not method definitions inside ?
Sergey Alexandrovich Kryukov 19-Sep-11 18:37pm    
Your understanding of interfaces it totally incorrect. Think about what you say -- if what you think was true, interfaces would be completely useless. You need to understand run-time types and compile-time types. In your sample, reader has compile time type of IDataReader, but its run-time type after assignment becomes the type of some concrete implementation class depending on concrete data provider. The goal is working with interface references, not with class references. As run-time types, they are always implementation class references.

Does it makes things more clear to you?
--SA
parmar_punit 26-May-11 9:54am    
The reference variable is created at the time you writing IdataReader reader = cmd.ExecuteReader();
It is possible to create a reference variable of Interface but you are not allowed to initiate an object..
and if you know the Fundamental of C++, Base type can refereed to its derived type...
here IDataReader is a base type of DataReader so it can easily point out to its derived class object...
Thanks & Regards,
Punit Parmar
Sergey Alexandrovich Kryukov 19-Sep-11 18:38pm    
Correct. I tried to explain it in some different way above, please see.
--SA
This is second time, i'm submitting my Solution....
this time i'm not updating the solution, but put a different solution here......


There is not any difference between using that two different code....
if you are familiar with the Fundamental of C++ then you should know about it that Base type (pointer variable) can refereed to its derived type object...
Here, IDataReader is a base type of DataReader class so it can easily point out to its derived class object...

Thanks & Regards
Punit Parmar
 
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