How to Transform a connected Architecture(Datareader) to a Dis Connected Architecture(Datatable).
In your Project if you use Connected Oriented Architecture(DataReader) suddenly you want to transform Dis Connected Oriented Architecture(DataTable) .In Asp.net there is a way you can transform a Connected Oriented Architecture(Data Reader) to Dis Connected Oriented Architecture (DataTable) with minimal of effort(less code changes). so how to Achieve this in your .Net Programming(vb/C#).I will explain it with Code Snippets.
As Mentioned in the Article Summary ADO.net Programming provides a way to transform a Data Reader(Connected Oriented Architecture) to Data Table(Disconnected Architecture).In .Net there is a Method that transform Connected Oriented Architecture to Dis Connected oriented Architecture that method is none other than Load().What are the arguments/Parameters that this Load method will offer for You i will explain below lines.
Before that to see the snippet of code every Programmer mind Churns with a Question why we need to change/transform/convert a Connected Oriented Architecture to Dis Connected Oriented Architecture below are the few reasons for it.Reasons for Transforming Connected Oriented Architecture(Data Reader) to Data Table(Dis Connected oriented Architecture) :
When you are working in a Maintenance Project in .Net as you have limitations to change the code very vastly and there use a DataReader to fetch the Huge amount of Data(Result set) that Database server is located far location from you and if you use connected Oriented Architecture and loop it to set the values.The web pages of your website will become very slow .
Due to Connected Oriented Architecture may leads to Network Traffic Congestion(heavy Traffic/jam/Blockage).
2)The Data Reader is Forward only way of fetching the data.But Datatable have a capacity to move the records both ways.
Here are the parameters that you pass in Load() and syntax of the Load() Method as given below
The DataTable class have a Load Method() The Load Method will take the Following Arguments/Parameters.
DataTable.Load(Datareader);
DataTable.Load(DataReader,LoadOption)
Using this Load Option (Preserve changes Default)if the Datatable already have rows than it will club/merge/append with existing rows with datareader rows.
DataTable.Load(DataReader,LoadOption,FillErrorEventHandler)
SqlDataReader dr =cmd.ExecuteReader();
DataTable dt =new Datatable()
dt.Load(dr )
Now see the below full code how to Use this Method in c#.
SqlConnection con =new SqlConnection("Connection String");
con.open();
SqlCommand cmd = new SqlCommand("select * from Tablename",con);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehaviour.CloseConnection);
Datatable dt =new Datatable();
dt.Load(rdr);
Note : Here the Datatable does n't have any Specified Schema or data when loading the records from Datareader it will take the Datareaders Schema.
Note :
Once you see the above code once the data fetched to the Datareader here i am are using Command Behaviour.CloseConnection this will automatically closes the Connection.
Let us see the Below run time screen shoot of sqldatareader.
see the below screen shot in Visual Studio.Net how the datatable is loading....