Thursday 25 July 2013

What is the difference between dataTable and dataset?


Dataset

DataSet objects are in-memory representations of data. They contain multiple Datatable objects, which contain columns and rows, just like normal database tables. You can even define relations between tables to create parent-child relationships. The DataSet is specifically designed to help manage data in memory and to support disconnected operations on data, when such a scenario make sense. The DataSet is an object that is used by all of the Data Providers, which is why it does not have a Data Provider specific prefix.

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