Wednesday, 18 April 2012

Dataset, Dataview, Datatable and common operations on them

DataTable:

• DataTable is a logical representation of data in terms of rows and columns.
• DataSet stores retrieved data from database into DataTables.
• If DataSet fetches data from more than one table, it stores them in different DataTables.

DataRow and DataColumn:

• DataRow and DataColumn are the components of DataTable.
• DataRow represents the row and DataColumn represents the column in DataTable.

DataSet:

• A DataSet is an in-memory representation of a database-like structure.
• Like database, it can have one or more DataTables.
• DataSet is an instance of the datasource like Oracle, SQL Server or Access. It uses disconnected access to database. First a connection is made to database,  then the required data is fetched from database and filled into the DataSet. After that, the connection to database is broken. In this way, using DataSets, saves bandwidth and load on database server.

How to declare and initialize DataSet in Delphi Prsim?

dsMyDataSet : DataSet;
DataSet dsMyDataSet := new DataSet;

How to fill data in DataSet from XML?

dsMyDataSet.ReadXM L(myXMLData);

Above code will fill data into dsMyDataSet from XML file. We can also convert our DataSet into XML file as

dsMyDataSet.WriteXM L(myXMLData);

Here myXMLData is the xml file name.

How to count number of tables contained in a DataSet?

dsMyDataSet.Tables.Count;

How to count number or rows contained in a particular DataTable of a DataSet?

dsMyDataSet.Tables[TableIndex].Rows.Count;  or
dsMyDataSet.Tables[‘TableName’].Rows.Count;

How to access any cell of a DataSet?

dsMyDataSet.Table[TableIndex].Rows [RowIndex].Item[‘ColumnName’].ToString;

How to delete a particular row of a DataSet?

dvMyDataView.Table.Rows [RowIndex].Delete;

How to filter a DataSet?

dsMyDataSet.Table[TableIndex].Select(“ColumnName = ColumnValue”);
For example:
dsMyDataSet.Table[TableIndex].Select(“ID = 123”);
dsMyDataSet.Table[TableIndex]. Select(“ID LIKE 123%”);
Assume ID as column of a DataSet.

How to merge two DataSets?

dsMyDataSet1.Merge(dsMyDataSet2);

DataView:

• A DataView is a logical structure through which we display data contained in the DataTable of DataSet by binding it to DataGrid, DropDownLists, TextBoxes, RatioButtons, CheckBoxes etc.
• A DataView can show the same data or less data than the DataSet. You should declare DataView for a particular DataSet. If  you don’t specify and DataView for your DataSet, a default DataView would get created for it. So if you are binding DataSet with DataGrid, it will not give any compile time or run time error but implicitly, dotnet framework converts that DataSet into dataset.defaultview(0).
• Unlike DataSet, DataView contains only one table.

How to declare and initialize DataView in Delphi Prism?

dvMyDataView : DataView;
DataView dvMyDataView := new DataView;

How to fill data in DataView from DataSet?
 
dvMyDataView := dsMyDataSet.Tables[‘TableName’].DefaultView;    or
dvMyDataView := dsMyDataSet.Tables[TableIndex].DefaultView;

Above code will fill data into dvMyDataView from a particular DataTable of DatSet dsMyDataSet. You can pass either DataTable name or DataTable index to DataSet to retrieve data from a particulat DataTable contained in that DataSet.

How to count number or rows contained in a DataView?

dvMyDataView.Table.Rows.Count;

How to access any cell of a DataView?

dvMyDataView.Table.Rows [RowIndex][‘ColumnName’].ToString;

How to delete a particular row of a DataView?

dvMyDataView.Table.Rows [RowIndex].Delete;

How to filter a DataView?

dvMyDataView.RowFilter := “ColumnName = ColumnValue”;
For example: dvMyDataView.RowFilter := “ID = 123”;
dvMyDataView.RowFilter := “ID LIKE 123%”;
Assume ID as column of a DataView.

Note: We can only filter row of DataView not column.

No comments:

Post a Comment