Thursday, January 7, 2010

What is the difference between datatable and dataset?

Dataset
dataset is collection of data table and it is provide interface between database and datatable. dataset consist multiple table. A Dataset is an in-memory representation of a collection of Database objects including tables of a relational database schema. Dataset represents an in-memory cache of data. The dataset contains a collection of Tables, Relations, constraints etc., The dataset contains all the objects as a Collection of objects. For example it contains the Database Tables as DataTable objects. Dataset is a collection of tables, which is used in disconnected architecture. Generally to fill dataset we use fill method of dataadapter.

It can be used for manipulating the data remotely and finally updating the database with the modified data. This way it enables disconnected means of working with data. This improves performance in terms of reducing the number of times a database is accessed for data manipulations.

Example for dataset:
using System.Data.SqlClient;
using System.Data;

string strDBConnection = "server=(local);database=DatabaseName;user id=UserName;password=Pwd;connection reset=false;connection lifetime=5;Trusted_Connection=Yes;"
SqlConnection dbConnection;
dbConnection = new SqlConnection(strDBConnection);

string strSelectSql = "Select * from [DatabaseName].[OwnerName].[TableName] order by FieldName";

//Open the connection
dbConnection.Open();

//Create a command
SqlCommand selectSqlCommand = new SqlCommand(strSelectSql,dbConnection);
SqlDataAdapter sqlData = new SqlDataAdapter(selectSqlCommand);
DataSet dsSelectData = new DataSet();
sqlData.Fill(dsSelectData);


Data Table
Data table is a collection of records that consist the single table. You can get datatable from dataset as follows.

DataTable tbl = DataSet.Tables["Tablename" or Table instance number]

Datatable represents one table of in-memory data. Datatable is a single table. datatable is just like table which has columns and rows. A DataTable is an in-memory representation of data, typically retrieved from a database or XML source.

No comments:

Post a Comment