- This topic explains about how to create dataset without using database in C# programming.
- Tools needed: RichTextBox1 (RichTextBox1).
What is Dataset? – create dataset without using database in C#
- Dataset represents a collection of data retrieved from the Data Source. Usually it is represented in tabular form. The DataSet contains DataTableCollection and their DataRelationCollection. Dataset can read the data in XML file.
Namespace: System.Data
Assembly: System.Data (in System.Data.dll)
- Syntax for DataSet class
// Declaration [SerializableAttribute()] public class DataSet : MarshalByValueComponent, IListSource { }
DataTable Class – create dataset without using database in C#
- DataTable class represents one table of in-memory data.
- Syntax
// Declaration [SerializableAttribute()] public class DataTable : MarshalByValueComponent, IListSource { }
DataColumn Class – create dataset without using database in C#
- DataColumn Class represents the schema of a column in a DataTable.
- Syntax
// Declaration public class DataColumn : MarshalByValueComponent { }
DataColumn Constructor (String, Type) – create dataset without using database in C#
- It inititalizes a new instance of the DataColumn class that used in specified column name and data type.
- Syntax
// Declaration public DummyClass(string columnName, Type dataType) { }
- String – Column name
- Type – Data type
How to create dataset without using database in C#– Complete Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable dt; DataRow dr; DataColumn idCoulumn; DataColumn nameCoulumn; int i; dt = new DataTable(); idCoulumn = new DataColumn("ID", Type.GetType("System.Int32")); nameCoulumn = new DataColumn("Name", Type.GetType("System.String")); dt.Columns.Add(idCoulumn); dt.Columns.Add(nameCoulumn); // adding datarow to datatable dr = dt.NewRow(); dr["ID"] = "1"; dr["Name"] = "Rahul"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = "2"; dr["Name"] = "Manu"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = "3"; dr["Name"] = "Ravi"; dt.Rows.Add(dr); // adding datatable to dataset ds.Tables.Add(dt); for (i = 0; (i <= (ds.Tables[0].Rows.Count - 1)); i++) { RichTextBox1.Text = (RichTextBox1.Text + (ds.Tables[0].Rows[i][0] + (" -- " + ds.Tables[0].Rows[i][1]))); RichTextBox1.Text = (RichTextBox1.Text + "\r\n"); } } } }