Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Retrieve data using dataset in c#

The DataSet Class


The System.Data.Dataset class holds data that is retrieved from the database. The DataSet class allows you to hold disconnected data. It means, you can work with the contents of the database while disconnected to the actual database. This is because the contents that you will be working on already exists inside the DataSet. You can consider the DataSet class as a mini database management system inside the memory.

The DataSet class has a Tables property which is a collection of systems.Data.DataTable objects. A typical database can contain multiple tables. Just like a database, a DataSet can contain multiple tables as well. Records retrieved from the actual database are stored as a DataTable and added to the Tables property of the DataSet. The DataTable also has properties named Columns and Rows which are collections of DataColumn and DataRow respectively. These properties contain the individual columns and rows of the table from the database. The good thing about the DataTable and DataColumn is that they can be assigned with names. This is done by using the TableName and ColumnName properties or using their constructors.

Let's create a simple DataTable and add it to the DataSet. Create a new Windows Forms Application and name it DataSetform. Add a DataGridView from the toolbar and set its Dock property to Fill.


Double click the title bar of the form to generate an event handler for the form's Load event. First, be sure to import the System. Data namespace. Use the following code for the Load event handler.


Lines 4-11 declares the different classes for creating a complete DataTable and DataSet.
Notice the passed names inside the constructor for the DataTable and each DataColumn. This will make them easier to reference when we call them using the DataSet
Lines 14-17 adds the created DataColumn object to the Columns property of the DataTable. It is important that you do this first before specifying the rows. Lines 20-22 initializes each DataRow object using the DataTable.NewRow() method which returns a DataRow object that already has the proper number of values it can have. Lines 25-35 assigns values to each field of the DataRow objects. Note that each field was accessed via indexers that accepts the name of the corresponding DataColumn. 
To complete the DataTable, lines 38-40 adds all the rows to the DataTable.Rows property. Finally, we added the DataTable object to the Tables property of the DataSet. To access the table that we have added, we can use an indexer for the Tables property and pass the name of the table.
We assign the table to the DataSource property of the DataGridView so it will show the contents of the DataTable that we have created. Run the program and you will see the records of the table inside the DataGridView control.

Do you know:-
  • The Command Class | C# | TrickCode
  • C# DataReader ADO.NET |Trickcode
  • What is disconnected data access?
  • how to Adding Parameters to Commands
  • SQL Basics?
  • What is Database Preparations?
  • SQL vs NoSQL or MySQL vs MongoDB
  • What is data provider in C#?
  • What is connection string C#?
  • [SQL] SELECT Statement | Example
  • how to get return value from stored procedure in sql server
  • how to prevent sql injection attacks
  • What is Database? What is SQL?
  • C#.Net How To: Send email in asp.net using c#
If you like the tutorial, then please share this tutorial with your friends on social media.


This post first appeared on Learn To Asp.net,Visual Code,Nodes,Angular,technews :TrickCode, please read the originial post: here

Share the post

How to Retrieve data using dataset in c#

×

Subscribe to Learn To Asp.net,visual Code,nodes,angular,technews :trickcode

Get updates delivered right to your inbox!

Thank you for your subscription

×