Visual C#

Microsoft Visual C# Data Sets: Introduction

A database is a list of items. The nature of the items is not particularly important because any list of any type of items constitutes a database. The…

2 min read476 words4 code samples

A Set of Data#

Introduction#

Essentially, any collection of items can be considered a database, provided it is organized for better management. Whether it's a list of people, countries, or products, the term "database" refers to how these items are formally stored, be it in memory or on a computer disk. Before you start building, it's important to decide on the types of data the list will contain. Each entry is known as a datum, and a group of such entries forms a data set.

Data Set Creation#

To support the creation and management of a set of data, the .NET Framework provides the DataSet class, which is defined in the System.Data namespace. Therefore, to create a list, you can start by declaring a variable of type DataSet. To initialize a DataSet variable, the class is equipped with four constructors, the first of which is the default, meaning it doesn't take any argument. The DataSet default constructor allows you to declare a variable without providing further information, just to let the compiler know that you are going to create or need a list of items. Here is an example:

C#
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;

namespace VideoCollection1
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnDocument_Click(object sender, EventArgs e)
        {
            DataSet dsVideoCollection = new DataSet();
        }
    }
}

If you are planning to use a DataSet object from more than one method or event, you can declare it globally, that is, in the class of a form. Here is an example:

C#
namespace VideoCollection1
{
    public partial class Exercise : Form
    {
        DataSet dsVideoCollection;

        public Exercise()
        {
            InitializeComponent();
        }

        private void btnDocument_Click(object sender, EventArgs e)
        {
            dsVideoCollection = new DataSet();
        }
    }
}

You can visually design a data set by using the DataSet tool found in the Data tab of the Toolbox. When you add this to a form, the Add Dataset dialog appears, where you can choose to create an Untyped Dataset.

Then click OK. After doing it, the compiler would programmatically declare a variable and give its name in the (Name) field of the Properties window.

The Name of a Data Set#

When creating a set of data, you must name it. This would allow you to refer to the set later on by a formal name. If you create a data set using the DataSet's default constructor, the compiler would give it a default name. To support the data set's object name, the DataSet class is equipped with a property named DataSetName. Therefore, if you create a data set using the class's default constructor, to give it a name, you can assign a string to the DataSetName property. If you visually create a data set, the Microsoft Visual Studio would create a default name for it in the DataSetName field of the Properties Windows. If you want to change that name, replace that property's value. To specify a data set's name when creating it, you can use the following constructor of the DataSet class:

C#
public DataSet(string dataSetName);

The formal name of the set must be provided as an argument to this constructor. The name can be anything you choose, as long as it follows C# identifier rules. For example:

C#
using System;
using System.Data;

public class VideoCollection
{
    public DataSet dsVideoCollection;

    public VideoCollection()
    {
        dsVideoCollection = new DataSet("Videos");
    }
}

Home Copyright © 2010-2011 C# Key Next