|
|
To make database development user friendly and
graphically-driven, Microsoft Visual Studio provides its own set of tools.
Some of these tools are available from the Toolbox. Some other tools are
provided as classes you can use. As an example, we defined a data set as a
system of values. The values are kept in one or more lists. We also saw
that, to support this system, the .NET Framework provides a class named
DataSet. This class is represented in the Data section of
the Toolbox of Microsoft Visual Studio by the object of the same name.
|
|
Practical
Learning: Introducing Visual Database Support
|
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click FILE -> New
-> Project...
- Make sure Windows Forms Application. Set the Name to
AltairRealtors2 and click OK
- To create a new database, in the Server Explorer, right-click Data
Connections and click Create New SQL Server Database...
- In the Server Name combo box, select your server or type (local)
- Set the database name as AltairRealtors2 and
click OK
- In the Server Explorer, right-click the AltairRealtors2 connection
and click New Query
- Open the
AltairRealtors2 file and select everything in the document
- Copy and paste it in the Query window
- Right-click inside the document and click Execute
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type AltairRealtors.cs and press Enter twice
Instead of using the DataSet object from the Toolbox,
Microsoft Visual Studio provides a technique that allows you to
automatically get a data set object by creating a connection to a
database. The data set would be filled with the tables from the database.
To use it, you can first display the Data Source window. To display the
Data Source, on the main menu, click Data -> Show Data Sources.
To create a data source:
- On the main menu, click PROJECT -> Add New Data Source...
- Display the Data Source window. In the Data Source window, click
Add New Data Source
In the first page of the Data Source Configuration
Wizard, you must specify the type of data source you want to create:

If you click Database and click Next, the second page
of the wizard allows you to create a data set:

The third page of the wizard allows you to select an
existing connection or create a new one. To select an existing connection,
you can click the arrow of the combo box and select from the list. If you
click the New Connection button, the Add Connection dialog box would come
up. If you have many server or you have Microsoft SQL Server installed on
many computers, click the arrow of the Server Name combo box and select
the name of the server:

If you are going to use a database on the same
computer where you are creating the application, type (local).
Next, in the Connect to a Database section, to select a database, click
the arrow of the Select or Enter a Database Name combo box to display the
list of available databases from the selected server. After making the
selections, you can click Test Connection to make sure the connection is
alright. Once everything is alright, click OK.
After making the selection, click Next. In the third
page of the wizard, you would specify a name for the connection string,
and click Next. In the fourth page of the wizard, you have the option of
selecting one or more tables (and other types of objects) to include in
your data set. To do this, you can click the check boxes in the list. If
you do not specify the tables, you can click Finish and, later on, you can
reconfigure the data source and select the tables (and/or other types of
objects). After making your selection, you can click Finish.
|
Practical
Learning: Adding a Data Source
|
|
- On the main menu, click PROJECT -> Add New Data Source...
- On the first page of the wizard, make sure Database is selected
and click Next
- On the second page of the wizard, make sure Dataset is selected
and click Next
- In the third page of the wizard, click New Connection...
If the
Choose Data Source dialog box comes up, click Microsoft SQL Server and
click Continue
- In the Server Name combo box, select the server or type
(local)
- In the Select or Enter a Database Name combo box, select
AltairRealtors2
- Click Test Connection
- Click OK twice
- On the Data Source Configuration Wizard, make sure the new
connection is selected
Click the + button of Connection String
- Click Next
- Change the connection string to csAltairRealtors
and click Next
- Click the check box of Tables
- Change the name of the data set to dsAltairRealtors

- Click Finish
|
The Characteristics of a Data Set
|
|
When you click the Finish button of the Data Source
Configuration Wizard, Microsoft Visual Studio generates many classes (XML
Schemas) and creates a data set object specially made and configured for
your database. Practically, the studio creates a class named after the
name you gave to the data set and this class would be derived from the
DataSet class. To examine this created class, from the Solution
Explorer, you can open the file that holds the name of the data set
followed by .Designer.cs.
Among the objects created in the data set class is a
class that represents the table (or each table) you selected in the last
page of the Data Source Configuration Wizard. This class for the table is
derived from the DataTable class and implements the
System.Collections.IEnumerable interface. In order to use
this new table in your code, you must declare a variable for it. Once you
have done that, you can access the characteristics (properties and
methods) of the table or its parent.
Although the data set created from the Toolbox and the
one generated from creating a data source have many differences, they
still share the common definition of being data sets. As mentioned
earlier, a data set created from adding a data source contains the
table(s) (including its (their) column(s) and record(s), if any) you would
have selected. This allows you to access any of the characteristics
of a data set.
Microsoft SQL Server does not provide user-friendly
objects that a user can use to perform the necessary operations of a
regular application. That is why you use Microsoft Visual Studio to create
an application made of good looking Windows controls. The controls of a
Windows application are meant to serve all types of applications, not just
databases. If you want a Windows control of your application to use the
values of your database, you must create a type of link between the
control and the column of a table. This process is referred to as binding.
The object that serves this connection is referred to as a binding source.
|
Creating a Binding Source
|
|
To support binding sources, the .NET Framework
provides the BindingSource class from the
System.Windows.Forms namespace. To visually create a binding
source, from the Data section of the Toolbox, click BindingSource
and click the form or container of your application. Because it is a
non-visual object, its label would be positioned under the form. You can
then specify its characteristics in the Properties window.
To programmatically create a binding source, declare a
variable of type BindingSource. The class is equipped
with three constructors. The default constructor allows you to simply
declare the variable. Here is an example:
private void btnBindingSource_Click(object sender, EventArgs e)
{
BindingSource bsNames = new BindingSource();
}
|
The Data Source of a Binding Source
|
|
If you create a binding source, obviously you must
give it a name. If you create it from the Toolbox, you can accept or
change its name in the Properties window. Besides its name, the second
most important detail of a binding source is the list that holds its
values. This list is referred to as the data source. To make the binding
source as flexible as possible, the data source can be almost any type of
list, including an array. In reality, any class that implements the
IList interface is a candidate to be a data source.
To support data sources, the BindingSource
class is equipped with a property named DataSource, which
is of type object. The idea of using the vague object
type indicates that many types, but not all types, of objects or lists can
be used as data sources.
To programmatically specify the data source of a
binding source, first create your list, then assign it to the
DataSource property of your BindingSource
object. Here is an example:
private void btnBindingSource_Click(object sender, EventArgs e)
{
BindingSource bsNames = new BindingSource();
List<string> strNames = new List<string>();
strNames.Add("Vicky Bisso");
strNames.Add("Amy Warren");
strNames.Add("Chrissie Childs");
strNames.Add("Martial Otto");
bsNames.DataSource = strNames;
}
To visually specify the data source of a binding
source, access the Properties window of your BindingSource object. In the
Properties window, click DataSource and click the arrow of its combo box:
- If you had not previously created a data source, click Add Project
Data Source... This would launch the Data Source Configuration Wizard
that allows you to create and configure a data set
- If you had previously created a data set object, expand the Other
Data Sources node and expand the Project Data Sources node. You would
see the created data set and you can select it. Here is an example:

|
Practical
Learning: Creating a Binding Source
|
|
- If necessary, display the form.
In the Toolbox, click Data and
click BindingSource
- Click the form
- In the Properties window, change its (Name) to
bsProperties
- Click DataSource and click the arrow of its combo box
- Click the + button of the Other Data Sources node to expand it
- Click the + button of the Project Data Sources node
- Click dsAltairRealtors