TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ADO.Net Dataset

ADO Net Dataset with introduction, data providers, sql server connectivity, connection, command, datareader, dataset, dataadapter, datatables, web form examples, mvc examples etc.

<< Back to ADO

ADO.NET DataSet

It is a collection of data tables that contain the data. It is used to fetch data without interacting with a Data Source that's why, it also known as disconnected data access method. It is an in-memory data store that can hold more than one table at the same time. We can use DataRelation object to relate these tables. The DataSet can also be used to read and write data as XML document.

ADO.NET provides a DataSet class that can be used to create DataSet object. It contains constructors and methods to perform data related operations.

DataSet Class Signature

public class DataSet : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, 
System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable, 
System.Xml.Serialization.IXmlSerializable

DataSet Constructors

Constructor Description
DataSet() It is used to initialize a new instance of the DataSet class.
DataSet(String) It is used to initialize a new instance of a DataSet class with the given name.
DataSet(SerializationInfo, StreamingContext) It is used to initialize a new instance of a DataSet class that has the given serialization information and context.
DataSet(SerializationInfo, StreamingContext, Boolean) It is used to initialize a new instance of the DataSet class.

DataSet Properties

Properties Description
CaseSensitive It is used to check whether DataTable objects are case-sensitive or not.
DataSetName It is used to get or set name of the current DataSet.
DefaultViewManager It is used to get a custom view of the data contained in the DataSet to allow filtering and searching.
HasErrors It is used to check whether there are errors in any of the DataTable objects within this DataSet.
IsInitialized It is used to check whether the DataSet is initialized or not.
Locale It is used to get or set the locale information used to compare strings within the table.
Namespace It is used to get or set the namespace of the DataSet.
Site It is used to get or set an ISite for the DataSet.
Tables It is used to get the collection of tables contained in the DataSet.

DataSet Methods

The following table contains some commonly used methods of DataSet.

Method Description
BeginInit() It is used to begin the initialization of a DataSet that is used on a form.
Clear() It is used to clear the DataSet of any data by removing all rows in all tables.
Clone() It is used to copy the structure of the DataSet.
Copy() It is used to copy both the structure and data for this DataSet.
CreateDataReader(DataTable[]) It returns a DataTableReader with one result set per DataTable.
CreateDataReader() It returns a DataTableReader with one result set per DataTable.
EndInit() It ends the initialization of a DataSet that is used on a form.
GetXml() It returns the XML representation of the data stored in the DataSet.
GetXmlSchema() It returns the XML Schema for the XML representation of the data stored in the DataSet.
Load(IDataReader, LoadOption, DataTable[]) It is used to fill a DataSet with values from a data source using the supplied IDataReader.
Merge(DataSet) It is used to merge a specified DataSet and its schema into the current DataSet.
Merge(DataTable) It is used to merge a specified DataTable and its schema into the current DataSet.
ReadXml(XmlReader, XmlReadMode) It is used to read XML schema and data into the DataSet using the specified XmlReader and XmlReadMode.
Reset() It is used to clear all tables and removes all relations, foreign constraints, and tables from the DataSet.
WriteXml(XmlWriter, XmlWriteMode) It is used to write the current data and optionally the schema for the DataSet using the specified XmlWriter and XmlWriteMode.

Example:

Here, in this example, we are implementing DataSet and displaying data into a gridview. Create a web form and drag a gridview from the toolbox to the form. We can find it inside the data category.

ADO Net Dataset 1

// DataSetDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataSetDemo.aspx.cs" 
Inherits="DataSetExample.DataSetDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
    </form>
</body>
</html>

CodeBehind

// DataSetDemo.aspx.cs

using System;
using System.Data.SqlClient;
using System.Data;
namespace DataSetExample
{
    public partial class DataSetDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection("data source=.; database=student; integrated security=SSPI"))
            {
                SqlDataAdapter sde = new SqlDataAdapter("Select * from student", con);
                DataSet ds = new DataSet();
                sde.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }
}

Output:

Execute this code by the combination of Ctrl+F5. It will produce the following output.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf