What is the difference between IDataReader and SqlDataReader?
SqlDataReader implements the IDataReader interface. The same goes for all other ADO.NET drivers (Oracle, MySql, etc.). You can use IDataReader, so if you plan to change the database engine someday, you don’t have to rewrite all your SqlDataReader references. The same goes for IDbConnection, IDbCommand, etc.
Table of Contents
What is IDataReader?
IDataReader refers to an interface. Basically, if your method accepts an IDataReader, that means it will accept anything that uses that interface. This means you can use any data reader you want basically. The method will accept its data reader because the data reader implements the IDataReader interface.
What does SqlDataReader do?
A SqlDataReader is a type that is good at reading data as efficiently as possible. You can *not* use it to write data. SqlDataReaders are often described as firehose-like fast forward data streams. You can read from SqlDataReader objects in a forward-only sequential manner.
What is the difference between SqlDataReader and SqlDataAdapter?
A SqlDataAdapter is usually used to populate a DataSet or DataTable, so it will have access to the data after its connection has been closed (disconnected access). The SqlDataReader is a wired, fast-forward-only cursor that tends to be generally faster than populating a DataSet/DataTable.
What is the difference between SqlDataReader and SqlDataAdapter?
How to convert C# list to idatareader?
I wrote about using SqlBulkCopy to quickly load .NET data into SQL Server database. This class needs an instance of DataTable or IDataReader as a source. You can convert a C# List to a DataTable (see Converting List To DataTable here). I will now show a couple of examples of how to convert List to IDataReader. 1.
How to convert sqldatareader to a list of objects?
Here’s a simple example that isn’t of a stored procedure, but the same applies other than setting the command type to the stored procedure in the command object. Ignore mHasException and mLastException, they come from a NuGet package in my signature.
How to get int values from sqldatareader?
assuming there is no longer GetInt32(string) in SqlDataReader; if there is any, just use another method name instead of Call ToString() instead of emitting the reader result. reader[0].ToString(); reader[1].ToString(); // etc… And if you want to get values of specific data types (int in your case), try the following:
Do you have to match DataReader with sqldatareader?
Note that you need to make your class match the data reader 100%, so adjust your query to suit or pass some kind of list to skip fields. With an SQLDataReader talking to a SQL Server database, you have a pretty good type match between .net and the database.