Monday, 5 March 2018

COMPONENTS OF CONNECTED AND DISCONNECTED ARCHITECTURE IN C#

Reference Link: http://www.c-sharpcorner.com/UploadFile/8a67c0/connected-vs-disconnected-architecture-in-C-Sharp/
You can use two C# objects to achieve this, first one is DataSet and other one is DataReader.

DataSet

DataSet

It is a type of disconnected architecture. Disconnected architecture means, you don’t need to connect always when you want to get data from the database. You can get data from dataset; basically DataSet is a collection of datatables. We can store the database table, view data in the DataSet and can also store the xml value in dataset and get it if required.

To achieve this you need to use DataAdapter which work as a mediator between Database and DataSet.

Example


public DataSet GetEmployeeData()
{
    SqlConnection conString = new SqlConnection("myconnection");
    conString.Open();
    SqlCommand cmdQuery = new SqlCommand("Select * from Employee", conString);
    SqlDataAdapter sda = new SqlDataAdapter(cmdQuery);
    DataSet dsData = new DataSet();
    sda.Fill(dsData);
    return dsData;
}  
DataReader

It is a connected architecture, which means when you require  data from the database you need to connect with database and fetch the data from there. You can use if you need updated data from the database in a faster manner. DataReader is Read/Forward only that means we can only get the data using this but we cannot update or insert the data into the database. It fetches the record one by one.

Example
static void HasRows(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;",connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}\t{1}", reader.GetInt32(0),reader.GetString(1));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close(); 
    }

For more details download the following PDF file
https://drive.google.com/file/d/1kuDA7y7MmQcvE9W5vq6bdN-_CRsNVD5R/view?usp=sharing
 

No comments:

Post a Comment

Convey your thoughts to authors.