Thursday, December 29, 2011

What are DataSets and DataAdapters ?

Datasets store a copy of data from the database tables. However, Datasets can not directly retrieve data from Databases. DataAdapters are used to link Databases with DataSets. If we see diagrammatically,
DataSets < ----- DataAdapters < ----- DataProviders < ----- Databases
DataSets and DataAdapters are used to display and manipulate data from databases.

Reading Data into a Dataset

To read data into Dataset, you need to:
  • Create a database connection and then a dataset object.
  • Create a DataAdapter object and refer it to the DB connection already created. Note that every DataAdapter has to refer to a connection object. For example, SqlDataAdapter refers to SqlDataConnection.
  • The Fill method of DataAdapter has to be called to populate the Dataset object.
We elaborate the above mentioned steps by giving examples of how each step can be performed:


1)      As we said, our first task is to create a connection to database. We would explore later that there is no need of opening and closing database connection explicitly while you deal with DataAdapter objects. All you have to do is, create a connection to database using the code like this:
SqlConnection con = new SqlConnection ("data source=localhost; uid= sa; pwd= abc; database=Northwind");
We would use Northwind database by using OleDbConnection. The Code would
Look like:


OleDbConnection con= new OleDbConnection ("Provider =Microsoft.JET.OLEDB.4.0;" + "Data Source=C:\\Program Files\\Microsoft Office\\Office\\Samples\\Northwind.mdb");


2)      Now, create a Dataset object which would be used for storing and manipulating data. You would be writing something like


DataSet myDataSet = new DataSet ("Northwind");
Since the name of source database is Northwind, we have passed the same name in the constructor.


3)      The DataSet has been created but as we said before, this DataSet object can not directly interact with Database. We need to create a DataAdapter object which would refer to the connection already created. The following line would declare a DataAdapter object:


OleDbAdapter myDataAdapter = new OleDbAdapter (CommandObject, con);


The above line demonstrates one of many constructors of OleDbAdapter class. This constructor takes a command object and a database connection object. The purpose of command object is to retrieve suitable data needed for populating DataSet. As we know SQL commands directly interacting with database tables, a similar command can be assigned to CommandObject.


OleDbCommand CommandObject = new OleDbCommand ("Select * from employee");


Whatever data you need for your Dataset should be retrieved by using suitable command here. The second argument of OleDbAdapter constructor is connection object con.


Alternative approach for initializing DataAdapter object:
Place a null instead of CommandObject while you initialize the OleDbAdapter object:


OleDbAdapter myDataAdapter = new OleDbAdapter (null, con);


Then you assign your query to the CommandObject and write:


myDataAdapter.SelectCommand = CommandObject;




4)      Now, the bridge between the DataSet and Database has been created. You can populate dataset by using the Fill command:


myDataAdapter.Fill (myDataSet, "EmployeeData");


The first argument to Fill function is the DataSet name which we want to populate. The second argument is the name of DataTable. The results of SQL queries go into DataTable. In this example, we have created a DataTable named EmployeeData and the values in this table would be the results of SQL query: "Select * from employee". In this way, we can use a dataset for storing data from many database tables.
5)      DataTables within a Dataset can be accessed using Tables. To access EmployeeData, we need to write:


myDataSet.Tables["EmployeeData"].


To access rows in each Data Table, you need to write:


myDataSet.Tables["EmployeeData].Rows




To summarize:

  • Datasets store a copy of data from the database tables.
  • Datasets can not directly retrieve data from Databases.
  • DataAdapters are used to link Databases with DataSets.
  • To populate dataset, db connection is created which is followed by creating a DataAdapter and calling its Fill method.
  • OleDbCommand class can be used for applying desired SQL command on DataAdapter object. Dataset would be populated according to the selection criteria given in this command.
  • DataTables contain results of SQL query.
  • Modifications in DataTable can be done which would be later written on database by GetChanges method of Dataset.



Friday, December 16, 2011

What is .NET Framework ?

There are two combined definitions for the .NET Framework:
It’s a Language-neutral Component Library: this is a collection of code that we can call on and it will do certain functionality for us.  A good example of this is: if I need to connect to some TCPIP network, I don’t have to learn TCPIP and how to use it in a networking protocol.  I can use a class or a component that is already programmed into the .NET framework class library, and simply pass it some information and it’ll do it for me.  So we go a number of : Code Modules: these things are organized in such a way that we can
  • find what we need and they are specialized to do everything from managing collections to managing network, connections, managing security on our code...etc
  • The second part of the definition of .NET Framework is that it’s an ExecutionEnvironment:
    1. CTS (common type system)
    2. CLR (common language runtime): it watches everything that happens in .NET, it protect us from writing bad code, protects us from a security standpoint from people who try to hack us, and controls the environment that we are running in: what can we do safely and what we can’t do safely.  Managing our objects in the memory.
The .NET Framework can be divided into four main components:
  • Common Language Runtime (it’s the brain of the .NET framework)
  • .NET Framework Class Library: this is all the code functionality that Microsoft preprogrammed for you.
  • ADO.NET (the data access portion of .NET: DATA/XML)
  • ASP.NET (the web portion of the .NET Framework)
        .Net framework architecture diagram:


What is .Net?

.NET is Microsoft's set of unified programming languages that allow developers to create enterprise-class Web and desktop applications across C#, VB.NET, ASP.NET and C++.NET. It includes a powerful set of libraries called the .NET Framework that makes it easier and faster to program than ever before.


What is .Net Platform?
Microsoft .NET is a software development platform based on virtual machine architecture. Dot Net Platform is:
  • Language Independent – dot net application can be developed different languages (such as C#, VB, C++, etc.)
  • Platform Independent – dot net application can be run on any operating system which has .net framework installed.
  • Hardware Independent – dot net application can run on any hardware configuration .      It allows us to build windows based application, web based application, web service, mobile application, etc.