Can someone give me explanation on how this works or a link that explains this Is it the Same as a SqlDataReader but it can be implemented for all types of Data like Oracle an Access or what...a lil cloudy on how i should use the IDataReader
This is untested. Though I hope it clarifies some for you.
// It's all about N-Tier architecture
// Business logic layer - Database independent (Ado.net data providers implement the DB interfaces - at least those that are built correctly) Interfaces are used extensively by objects in the system.data namespace such as datasets etc.
// In this tier their is no Database type declaration System.Data.SqlClient, or System.Data.OleDb or .Net Oracle provider etc.
public
class Sample {
public void GetData(Int32 CustomerID, string ConnectionString){
DataLayer myDB = new DataLayer();
DBReaderResponse Rdr = myDB.GetCustomerRecords(CustomerID, ConnectionString);
System.Collections.Specialized.
StringCollection customerNames = new System.Collections.Specialized.StringCollection();
string recordName;
if (Rdr.Reader){
// Build ouput
while (Rdr.Reader.Read){
recordName = Rdr(
"CustomrName");
customerNames.Add(recordName);
}
Rdr.Reader.Close();
Rdr.Connection.Close();
Rdr.Connection.Dispose();
Rdr.Command.Dispose();
}
}
}
//########################################################
// Data Layer - alternate library (this one is for MS SQL)
// This Return Type could have an implements interface declaration
// where the interface defined three properties for each of the members
// The return type on the function would then be of the Interface type and not the object type
public class DBReaderResponse
{
public System.Data.IDataReader Reader;
public System.Data.IDbConnection Connection;
public System.Data.IDbCommand Command;
}
public class DataLayer
{
public DBReaderResponse GetCustomerRecords(Int32 CustID, String ConnectionString) {
System.Data.SqlClient.
SqlConnection Connection = new System.Data.SqlClient.SqlConnection(ConnectionString);
System.Data.SqlClient.
SqlCommand Command = new System.Data.SqlClient.SqlCommand(Connection);
DBReaderResponse DBResponse = new DBReaderResponse();
There is no *object* called IDataReader. It's just an interface. If you get an IDataReader as the return from some method, it's really a derived class, an SqlDataReader or an OracleDataReader or an OleDbDataReader or something.
You need to study the subject of interfaces in C#/.Net
As you may have noticed I have used rdr.Reader("Columnname"); This will compile with the VB compiler, but it shouldn't, as it may fail when an alternate to the SQLProvider is used.
You must use the available Interface objects - properties and functions of the IDataReader interface. Which are a little more cumbersome, but the logic still applies.
"So could i create a component using the IDataReader and use it for any data provider "
If your datalayer accepts the same function level arguments and returns the same result set then when using this approach only the data layer needs to be coded for alternate physical databases - so yes, the data provider is not visible to higher application layers.
"Where are you getting your IDataReader "
It is a member of the Data Layer return object DBReaderResponse. Declared with members that are implementations of the various interfaces (IDataReader). As such the consuming function can reference the known properties and methods as defined by the interface specification.
In the above, the Datalayer does not consume objects from the upper layer. The upperlayer consumes objects from the datalayer and the interface would consume objects from the business logic layer.
IDataReader???
alan_b
>> My IDataReader is a memeber of System.Data, in .Net 1.1
You seem to be missing the point. It would be impossible for you to have this in your code:
IDataReader myDataReader = new IDataReader()
because that will not compile because IDataReader is an interface. What you do have is something like this:
IDataReader myDataReader = new SqlDataReader();
But, mostly likely what you really have is
IDataReader myDataReader = SomeMethodWhichReturnsAnIDataReader();
That's the line we need to see, i.e., we need to know where you are getting your IDataReader from.
e.neu
This is untested. Though I hope it clarifies some for you.
// It's all about N-Tier architecture // Business logic layer - Database independent (Ado.net data providers implement the DB interfaces - at least those that are built correctly) Interfaces are used extensively by objects in the system.data namespace such as datasets etc. // In this tier their is no Database type declaration System.Data.SqlClient, or System.Data.OleDb or .Net Oracle provider etc.public
class Sample { public void GetData(Int32 CustomerID, string ConnectionString){ DataLayer myDB = new DataLayer(); DBReaderResponse Rdr = myDB.GetCustomerRecords(CustomerID, ConnectionString);System.Collections.Specialized.
StringCollection customerNames = new System.Collections.Specialized.StringCollection(); string recordName; if (Rdr.Reader){ // Build ouput while (Rdr.Reader.Read){recordName = Rdr(
"CustomrName");customerNames.Add(recordName);
}
Rdr.Reader.Close();
Rdr.Connection.Close();
Rdr.Connection.Dispose();
Rdr.Command.Dispose();
}
}
}
//######################################################## // Data Layer - alternate library (this one is for MS SQL) // This Return Type could have an implements interface declaration // where the interface defined three properties for each of the members // The return type on the function would then be of the Interface type and not the object type public class DBReaderResponse{
public System.Data.IDataReader Reader; public System.Data.IDbConnection Connection; public System.Data.IDbCommand Command;}
public class DataLayer{
public DBReaderResponse GetCustomerRecords(Int32 CustID, String ConnectionString) {System.Data.SqlClient.
SqlConnection Connection = new System.Data.SqlClient.SqlConnection(ConnectionString);System.Data.SqlClient.
SqlCommand Command = new System.Data.SqlClient.SqlCommand(Connection); DBReaderResponse DBResponse = new DBReaderResponse();System.Data.SqlClient.
SqlDataReader Reader;DBResponse.Command = Command;
DBResponse.Connection = Connection;
try{ Connection.Open();
DBResponse.Reader = Command.EndExecuteReader();
}
catch (exception ex){
// Handle ExceptionConnection.Dispose();
Command.Dispose();
}
return DBResponse;}
}
Michael_Shao
As stated this is only an illustration on interface usage.
The SQL command in the data layer is not fully configured.
commandtype and parameters etc.
Martin.
Rajeev Vandakar
Where are you getting your IDataReader
allken
My IDataReader is a memeber of System.Data, in .Net 1.1
public abstract interface IDataReader : System.IDisposable , System.Data.IDataRecord Member of System.Data
Jamie Thomson
There is no *object* called IDataReader. It's just an interface. If you get an IDataReader as the return from some method, it's really a derived class, an SqlDataReader or an OracleDataReader or an OleDbDataReader or something.
You need to study the subject of interfaces in C#/.Net
Soft-Landing
Sorry people it gets a little more complicated.
As you may have noticed I have used rdr.Reader("Columnname"); This will compile with the VB compiler, but it shouldn't, as it may fail when an alternate to the SQLProvider is used.
You must use the available Interface objects - properties and functions of the IDataReader interface. Which are a little more cumbersome, but the logic still applies.
"So could i create a component using the IDataReader and use it for any data provider "
If your datalayer accepts the same function level arguments and returns the same result set then when using this approach only the data layer needs to be coded for alternate physical databases - so yes, the data provider is not visible to higher application layers.
"Where are you getting your IDataReader "
It is a member of the Data Layer return object DBReaderResponse. Declared with members that are implementations of the various interfaces (IDataReader). As such the consuming function can reference the known properties and methods as defined by the interface specification.
In the above, the Datalayer does not consume objects from the upper layer. The upperlayer consumes objects from the datalayer and the interface would consume objects from the business logic layer.
dgorini
I knew where you were getting your IDataReader. I needed to know where Tryin2Bgood got his......
Dinakar.babu
So could i create a component using the IDataReader and use it for any data provider