|
Microsoft SQL Server Graphical Database Example : The Ceil Inn Hotel |
|
|
Ceil Inn is a (fictional) hotel. The company provides
various types rooms (bedrooms, suites, and conference rooms). Rooms have
different types of bed (queen, double, or king). Depending on the type of
room and the bed(s), a certain rate is charged to occupy or use a room.
|
We are asked to create a database application that
would help the employees of Ceil Inn to manage their business.
Practical
Learning: Starting the Application
|
|
- Start Microsoft Visual Studio
- To start a new project, on the main menu, click File -> New ->
Project...
- Make sure Windows Forms Application is selected.
Set the Name
to CeilInn1
- Click OK
- CIn the Solution Explorer, right-click Form1.cs and click Rename
- Type CeilInn.cs and press Enter
- Double-click the body of the form to generate its Load event
- Implement the event as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class CeilInn : Form
{
public CeilInn()
{
InitializeComponent();
}
private void CreateDatabase()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Integrated Security=Yes"))
{
SqlCommand cmdCeilInn =
new SqlCommand("IF EXISTS (" +
"SELECT name " +
"FROM sys.databases " +
"WHERE name = N'CeilInn1')" +
"DROP DATABASE CeilInn1; " +
"CREATE DATABASE CeilInn1;", cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
}
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCeilInn =
new SqlCommand("CREATE SCHEMA HumanResources;", cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
}
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCeilInn =
new SqlCommand("CREATE SCHEMA Hotel;", cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
}
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCeilInn =
new SqlCommand("CREATE SCHEMA Room;", cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
}
MessageBox.Show("The CeilInn1 database has been created.",
"Ceil Inn - Hotel",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void CeilInn_Load(object sender, EventArgs e)
{
CreateDatabase();
}
}
}
- Execute the application
- When the message box displays, click OK
The Employees of the Company
|
|
Employees are people who will manage all aspects of
the database. In most databases, they are identified with as much
information as possible. To keep our implication simple, we will need the
name and the title of each employee. To uniquely identity an employee,
each one will have an employee number. This number will be specified but
the person who is creating the record for a new hire.
Practical
Learning: Creating the Employees
|
|
- Change the document as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class CeilInn : Form
{
public CeilInn()
{
InitializeComponent();
}
private void CreateEmployees()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCeilInn =
new SqlCommand("CREATE TABLE HumanResources.Employees" +
"(" +
" EmployeeNumber nvarchar(10) NOT NULL," +
" FirstName nvarchar(24)," +
" LastName nvarchar(24) not null," +
" EmployeeName AS (LastName + N', ' + FirstName)," +
" Title nvarchar(50)," +
" Notes nvarchar(max)," +
" Constraint PK_Employees Primary Key (EmployeeNumber)" +
");",
cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
}
MessageBox.Show("The Employees table has been added to te CeilInn1 database.",
"Ceil Inn - Hotel",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void CeilInn_Load(object sender, EventArgs e)
{
CreateEmployees();
}
}
}
- Execute the application
- When the message box displays, click OK
- To start a new form, on the main menu, Project -> Add Windows
Form...
- Set the Name to NewEmployee
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
Other Properties |
Label |
|
|
Employee # |
|
TextBox |
|
txtEmployeeNumber |
|
|
Label |
|
|
First Name: |
|
TextBox |
|
txtFirstName |
|
|
Label |
|
|
Last Name: |
|
TextBox |
|
txtLastName |
|
|
Label |
|
|
Title: |
|
TextBox |
|
txtTitle |
|
|
Label |
|
|
Notes: |
|
TextBox |
|
txtNotes |
|
Multine: True Scrollbars: Vertical |
Form |
|
|
|
AcceptButton: btnOK CancelButton: btnCancel |
|
- On the main menu, click Project -> Add Windows Form...
- Set the Name to Employees
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
Anchor |
DataGridView |
|
dgvEmployees |
|
Top, Bottom, Left, Right |
Button |
|
btnNewEmployee |
New Employee... |
Bottom, Right |
Button |
|
btnClose |
Close |
Bottom, Right |
|
- Double-click an unoccupied area of the form
- Implement the event as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class Employees : Form
{
public Employees()
{
InitializeComponent();
}
private void ShowEmployees()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source='" + strServerName + "';" +
"Database='CeilInn1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdEmployees =
new SqlCommand(("SELECT EmployeeNumber," +
" FirstName, LastName," +
" EmployeeName, Title " +
"FROM HumanResources.Employees;",
cntCeilInn);
SqlDataAdapter sdaEmployees = new SqlDataAdapter();
DataSet dsEmployees = new DataSet("Employees");
cntCeilInn.Open();
sdaEmployees.SelectCommand = cmdEmployees;
sdaEmployees.Fill(dsEmployees);
dgvEmployees.DataSource = dsEmployees.Tables[0];
}
}
private void Employees_Load(object sender, EventArgs e)
{
ShowEmployees();
}
}
}
- Return to the form and double-click the New Employee button
- Implement the event as follows:
private void btnNewEmployee_Click(object sender, EventArgs e)
{
string strServerName = "EXPRESSION";
NewEmployee empl = new NewEmployee();
if (empl.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrEmpty(empl.txtEmployeeNumber.Text))
{
MessageBox.Show("You must specify the employee number.",
"Ceil Inn Hotel - Employees",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (string.IsNullOrEmpty(empl.txtLastName.Text))
{
MessageBox.Show("You must enter the employee's last name.",
"Ceil Inn Hotel - Employees",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using (SqlConnection cntLambdaSquare =
new SqlConnection("Data Source='" + strServerName + "';" +
"Database=CeilInn1;" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdLambdaSquare =
new SqlCommand("INSERT INTO HumanResources.Employees(" +
" EmployeeNumber, FirstName, LastName, Title, Notes) " +
"VALUES('" + empl.txtEmployeeNumber.Text + "', '" +
empl.txtFirstName.Text + "', '" + empl.txtLastName.Text +
"', '" + empl.txtTitle.Text + "', '" + empl.txtNotes.Text + "');",
cntLambdaSquare);
cntLambdaSquare.Open();
cmdLambdaSquare.ExecuteNonQuery();
MessageBox.Show("A new employee has been hired.",
"Ceil Inn Hotel - Employees",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
ShowEmployees();
}
- Return to the form and double-click the Close button
- Implement the event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
The Customers of the Hotel
|
|
The customers are the people who rent the rooms that
the hotel offers. Like employees, customers have to be identified. For our
database, we will just need each customer's name, the telephone number,
and information about a person to contact in case of emergency.
When a customer comes to rent a room, an account will
be created for him or her. If the same customer comes to rent a room
another time, a new accuont will be created. This means that, for
accounting purposes, we will need a unique account number for each renting
session, even for a repeating customer.
Practical
Learning: Creating the Customers
|
|
- Access the CeilInn.cs file and change its document as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class CeilInn : Form
{
public CeilInn()
{
InitializeComponent();
}
private void CreateCustomers()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCeilInn =
new SqlCommand("CREATE TABLE Hotel.Customers " +
"( " +
" AccountNumber nvarchar(12) not null," +
" FirstName nvarchar(24)," +
" LastName nvarchar(24) not null," +
" CustomerName AS (LastName + N', ' + FirstName)," +
" PhoneNumber nvarchar(20)," +
" EmergencyName nvarchar(50)," +
" EmergencyPhone nvarchar(20)," +
" Notes nvarchar(max)," +
" Constraint PK_Customers Primary Key (AccountNumber)" +
");",
cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
}
MessageBox.Show("The Customers table has been added to te CeilInn1 database.",
"Ceil Inn - Customers",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void CeilInn_Load(object sender, EventArgs e)
{
CreateCustomers();
}
}
}
- Execute the application
- When the message box displays, click OK
- To create a new form, on the main menu, Project -> Add Windows
Form...
- Set the Name to NewCustomer
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
Other Properties |
Label |
|
|
Account #: |
|
TextBox |
|
txtAccountNumber |
|
|
Label |
|
|
First Name: |
|
TextBox |
|
txtFirstName |
|
|
Label |
|
|
Last Name: |
|
TextBox |
|
txtLastName |
|
|
Label |
|
|
Emrg Name: |
|
TextBox |
|
txtEmergencyName |
|
|
Label |
|
|
Emrg Phone: |
|
TextBox |
|
txtEmergencyPhone |
|
|
Label |
|
|
Notes: |
|
TextBox |
|
txtNotes |
|
Multine: True Scrollbars: Vertical |
Form |
|
|
|
AcceptButton: btnOK CancelButton: btnCancel |
|
- On the main menu, click Project -> Add Windows Form...
- Set the Name to Customers
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
Anchor |
DataGridView |
|
dgvCustomers |
|
Top, Bottom, Left, Right |
Button |
|
btnNewCustomer |
New Customer... |
Bottom, Right |
Button |
|
btnClose |
Close |
Bottom, Right |
|
- Double-click an unoccupied area of the form
- Implement the event as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class Customers : Form
{
public Customers()
{
InitializeComponent();
}
private void ShowCustomers()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source='" + strServerName + "';" +
"Database='CeilInn1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdCustomers =
new SqlCommand("SELECT AccountNumber, FirstName," +
" LastName, CustomerName," +
" PhoneNumber, EmergencyName," +
" EmergencyPhone " +
"FROM Hotel.Customers; ",
cntCeilInn);
SqlDataAdapter sdaCustomers = new SqlDataAdapter();
DataSet dsCustomers = new DataSet("Employees");
cntCeilInn.Open();
sdaCustomers.SelectCommand = cmdCustomers;
sdaCustomers.Fill(dsCustomers);
dgvCustomers.DataSource = dsCustomers.Tables[0];
}
}
private void Customers_Load(object sender, EventArgs e)
{
ShowCustomers();
}
}
}
- Return to the form and double-click the New Customer button
- Implement the event as follows:
private void btnNewCustomer_Click(object sender, EventArgs e)
{
string strServerName = "EXPRESSION";
NewCustomer client = new NewCustomer();
if (client.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrEmpty(client.txtAccountNumber.Text))
{
MessageBox.Show("You must specify an account number of the new customer.",
"Ceil Inn Hotel - Customers",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (string.IsNullOrEmpty(client.txtLastName.Text))
{
MessageBox.Show("You must enter the customer's last name.",
"Ceil Inn Hotel - Customers",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (string.IsNullOrEmpty(client.txtEmergencyPhone.Text))
{
MessageBox.Show("You must specify an emergency phone number for the customer.",
"Ceil Inn Hotel - Customers",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source='" + strServerName + "';" +
"Database=CeilInn1;" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdCeilInn =
new SqlCommand("INSERT INTO Hotel.Customers( " +
"AccountNumber, FirstName, LastName, " +
"PhoneNumber, EmergencyName," +
"EmergencyPhone, Notes) " +
"VALUES('" + client.txtAccountNumber.Text + "', '" +
client.txtFirstName.Text + "', '" + client.txtLastName.Text +
"', '" + client.txtPhoneNumber.Text + "', '" +
client.txtEmergencyName.Text + "', '" + client.txtEmergencyPhone.Text
+ "', '" + client.txtNotes.Text + "');",
cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
MessageBox.Show("A new customer has been registered.",
"Ceil Inn Hotel - Customers",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
ShowCustomers();
}
- Return to the form and double-click the Close button
- Implement the event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
The Ceil Inn hotel offers different categories of
rooms. A regular bedroom is equipped with one or two beds, one table, one
sofa or recliner, two chairs, a closet, and an iron. Besides being a place
to sleep, the room has a telephone (analogue line), a kitchen area that
has a stove, a refrigerator, a dish washer, and cabinets. The cabinets
contain spoons, forks, knives, and plates.
Most of the business of Ceil Inn hotel is based on
rooms the customers rent, and most customer rent a room to spend the
night. To serve such customers, bedroom have different types of bed. The
most common bed has a queen size that can serve one or two people. Another
category of room uses a king size, which is larger and wider than the
queen. Some customers wants a single room but with different beds. Such
rooms have a double bed. Of course, conference rooms do not have a bed.
The Occupancy Status of a Room
|
|
The hoteil has different rooms that are rented or
freed some time to time. When a room is not rented, it has the status of
available. When a room has been rented, its occupancy status must be
changed to occupied. To take care of this, we will create a table for
rooms status.
This is the idea behind a relational database. A
relational database is an application that contains two or more tables so
that information in one table is made available to another table or other
tables that need(s) it. The information is entered once in one particular
table. If the same information is needed in another table, it is simply
identified one way or another. This reduces, and can eliminate, the
likelihood of mistakes that result from duplicate data.
Rooms are probably the most important objects of a
hotel. A room is primarily characterized by its category as a bedroom or a
conference room. If it's a bedroom, then other pieces of information are
necessary. We have already seen what they are: bed type and status.
Each room must have a unique room number. The rate
applied when renting a room should be specified. After all, en employee
should not guess the price of a room when a customer is about to rent it.
Practical
Learning: Creating the Rooms
|
|
- Access the CeilInn.cs file and change the document as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class CeilInn : Form
{
public CeilInn()
{
InitializeComponent();
}
private void CreateHotelRooms()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCeilInn =
new SqlCommand("CREATE TABLE Hotel.Rooms" +
"(" +
" RoomNumber nvarchar(10) not null," +
" RoomType nvarchar(20) default N'Bedroom'," +
" BedType nvarchar(40) default N'Queen'," +
" Rate money default 85.95," +
" RoomStatus nvarchar(25) default N'Available'," +
" Constraint PK_Rooms Primary Key (RoomNumber)" +
");",
cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
}
MessageBox.Show("A table for the hotel rooms has been created.",
"Ceil Inn - Hotel Rooms",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void CeilInn_Load(object sender, EventArgs e)
{
CreateHotelRooms();
}
}
}
- Execute the application
- When the message box displays, click OK
- On the main menu, Project -> Add Windows
Form...
- Set the Name to NewHotelRoom
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
DropDownStyle |
Other Properties |
Label |
|
|
Room #: |
|
|
TextBox |
|
txtRoomNumber |
|
|
|
Label |
|
|
Room Type: |
|
|
ComboBox |
|
txtRoomTypes |
|
DropDownList |
Items: Studio Bedroom Conference Room |
Label |
|
|
Bed Type: |
|
|
ComboBox |
|
txtBedTypes |
|
DropDownList |
Items: King Queen 1-Double 2-Double |
Label |
|
|
Rate: |
|
|
TextBox |
|
txtRate |
85.95 |
|
TextAlign: Right |
Label |
|
|
Room Status: |
|
|
ComboBox |
|
cbxRoomsStatus |
|
DropDownList |
Items: Available Not Ready |
Label |
|
|
Notes: |
|
|
TextBox |
|
txtNotes |
|
|
Multine: True Scrollbars: Vertical |
Form |
|
|
|
|
AcceptButton: btnOK CancelButton: btnCancel |
|
- On the main menu, click Project -> Add Windows Form...
- Set the Name to HotelRooms
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
Anchor |
DataGridView |
|
dgvRooms |
|
Top, Bottom, Left, Right |
Button |
|
btnNewRoom |
New Room... |
Bottom, Right |
Button |
|
btnClose |
Close |
Bottom, Right |
|
- Double-click an unoccupied area of the form
- Implement the event as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class HotelRooms : Form
{
public HotelRooms()
{
InitializeComponent();
}
private void ShowRooms()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source='" + strServerName + "';" +
"Database='CeilInn1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdRooms =
new SqlCommand("SELECT RoomNumber, RoomType, BedType, " +
" Rate, RoomStatus, Notes " +
"FROM Hotel.Rooms; ",
cntCeilInn);
SqlDataAdapter sdaRooms = new SqlDataAdapter();
DataSet dsRooms = new DataSet("Rooms");
cntCeilInn.Open();
sdaRooms.SelectCommand = cmdRooms;
sdaRooms.Fill(dsRooms);
dgvRooms.DataSource = dsRooms.Tables[0];
}
}
private void HotelRooms_Load(object sender, EventArgs e)
{
ShowRooms();
}
}
}
- Return to the form and double-click the New Room button
- Implement the event as follows:
private void btnNewRoom_Click(object sender, EventArgs e)
{
string strServerName = "EXPRESSION";
NewHotelRoom rm = new NewHotelRoom();
if (rm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrEmpty(rm.txtRoomNumber.Text))
{
MessageBox.Show("You must specify the room number.",
"Ceil Inn Hotel - Rooms",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source='" + strServerName + "';" +
"Database=CeilInn1;" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdCeilInn =
new SqlCommand("INSERT INTO Hotel.Rooms( " +
"RoomNumber, RoomType, BedType, " +
"Rate, RoomStatus, Notes) " +
"VALUES('" + rm.txtRoomNumber.Text + "', '" +
rm.cbxBedTypes.Text + "', '" + rm.cbxBedTypes.Text +
"', " + double.Parse(rm.txtRate.Text) + ", '" +
rm.cbxRoomsStatus.Text + "', '" + rm.txtNotes.Text + "');",
cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
MessageBox.Show("A new Room has been built.",
"Ceil Inn Hotel - Customers",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
ShowRooms();
}
- Return to the form and double-click the Close button
- Implement the event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
After registering a customer, an employee can assign
a room. Normally, th employee would ask what type of room the customer
wants to rent. After all, some people want a bedroom, others a studio, and
others a conference room. The employee can use a form that would
show the list of available rooms.
When assigning a room to a customer, we first need to
know who (what employee) performed this operation. We also would like to
know the date this operation was performed. We may just use that same
date as the first day a customer rented a room or the day a conference
room was used. Of course, the employee needs to identify the customer who
is renting the room. The employee must also specify the room that is being
rented and how much will be charged. To know the rate, the employee can
use a form that shows available rooms.
In our hotel, the Internet is free to all customers
(we remember when hotels used to charge for Internet use; most of them now
offer free Internet and many of them have computers somewhere at the
receiption or in some room for customers use). If using the telephone in
the room, if a customer makes local calls, they are free. Long distance
calls are not free. We need to keep, on a daily basis, a calendar and rate
of phone consumption (this is one of the reasons we create a new account
for every customer, including returning customers).
As you can imagine, each room assignment must have a
unique number that identifies its record. We will use an automatically
incrementing number. To make it a little fancy, we will start the
numbering at 100001 and increment by 1 for each subsequent record.
Practical
Learning: Assigning a Room
|
|
- Access the CeilInn.cs file and change the document as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class CeilInn : Form
{
public CeilInn()
{
InitializeComponent();
}
private void CreateRoomsAssignments()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCeilInn =
new SqlCommand("CREATE TABLE Room.Assignments" +
"(" +
" AssignmentNumber int identity(100001, 1) NOT NULL," +
" ProcessedBy nvarchar(10)," +
" DateOccupied Date not null," +
" ProcessedFor nvarchar(12)," +
" RoomNumber nvarchar(10)," +
" RateApplied money default 0," +
" PhoneCharge money," +
" Notes nvarchar(max)," +
" Constraint PK_Occupancies Primary Key (AssignmentNumber)," +
" Constraint FK_Registrant Foreign Key(ProcessedBy) " +
" References HumanResources.Employees(EmployeeNumber), " +
" Constraint FK_Customers Foreign Key(ProcessedFor) " +
" References Hotel.Customers(AccountNumber), " +
" Constraint FK_Rooms Foreign Key(RoomRented) " +
" References Hotel.Rooms(RoomNumber) " +
");",
cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
}
MessageBox.Show("A table used to keep records of rooms assigned to customers has been created.",
"Ceil Inn - Hotel Rooms",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void CeilInn_Load(object sender, EventArgs e)
{
CreateRoomsAssignments();
}
}
}
- Execute the application
- When the message box displays, click OK
- To launch a new form, on the main menu, Project -> Add Windows
Form...
- Set the Name to RoomAssignment
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
DropDownStyle |
Other Properties |
Label |
|
|
Processed By |
|
BackColor: Gray BorderStyle: FixedSingle |
Label |
|
|
Employee #: |
|
|
TextBox |
|
txtEmployeeNumber |
|
|
|
TextBox |
|
txtEmployeeName |
|
|
|
Label |
|
|
Date Processed: |
|
|
DateTimePicker |
|
dtpDateProcessed |
|
|
|
Label |
|
|
Processed For |
|
BackColor: Gray BorderStyle: FixedSingle |
Label |
|
|
Customer Acnt #: |
|
|
TextBox |
|
txtAccountNumber |
|
|
|
TextBox |
|
txtCustomerName |
|
|
|
Label |
|
|
Room Assigned: |
|
|
TextBox |
|
txtRoomNumber |
|
|
Multine: True Scrollbars: Vertical |
TextBox |
|
txtRoomDetails |
|
|
|
Label |
|
|
Phone Charge |
|
|
TextBox |
|
txtPhoneCharge |
|
|
|
Label |
|
|
Rate Applied |
|
|
TextBox |
|
txtRateApplied |
|
|
TextAlign: Right |
Label |
|
|
Notes: |
|
|
TextBox |
|
txtNotes |
|
|
Multine: True Scrollbars: Vertical |
Form |
|
|
|
|
AcceptButton: btnOK CancelButton: btnCancel |
|
- On the form, click the txtEmployeeNumber text box
- In the Properties window, click Events and double Leave
- Implement the event as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class RoomAssignment : Form
{
public RoomAssignment()
{
InitializeComponent();
}
private void txtEmployeeNumber_Leave(object sender, EventArgs e)
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
return;
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdEmployees =
new SqlCommand("SELECT * FROM HumanResources.Employees " +
"WHERE EmployeeNumber = '" + txtEmployeeNumber.Text + "';",
cntCeilInn);
SqlDataAdapter sdaEmployees = new SqlDataAdapter();
DataSet dsEmployees = new DataSet("Employees");
cntCeilInn.Open();
sdaEmployees.SelectCommand = cmdEmployees;
sdaEmployees.Fill(dsEmployees);
foreach (DataRow drEmployee in dsEmployees.Tables[0].Rows)
txtEmployeeName.Text = (drEmployee["EmployeeName"].ToString());
}
}
}
}
- Return to the form and click the txtAccountNumber text box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows:
private void txtAccountNumber_Leave(object sender, EventArgs e)
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
if (string.IsNullOrEmpty(txtAccountNumber.Text))
return;
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCustomers =
new SqlCommand("SELECT * FROM Hotel.Customers " +
"WHERE AccountNumber = '" + txtAccountNumber.Text + "';",
cntCeilInn);
SqlDataAdapter sdaCustomers = new SqlDataAdapter();
DataSet dsCustomers = new DataSet("Customers");
cntCeilInn.Open();
sdaCustomers.SelectCommand = cmdCustomers;
sdaCustomers.Fill(dsCustomers);
foreach (DataRow drCustomer in dsCustomers.Tables[0].Rows)
txtCustomerName.Text = (drCustomer["CustomerName"].ToString());
}
}
- Return to the form and click the txtRoomNumber text box
- In the Events section of the Properties window, double-Leave
- Implement the event as follows:
private void txtRoomNumber_Leave(object sender, EventArgs e)
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
if (string.IsNullOrEmpty(txtRoomNumber.Text))
return;
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source=" + strServerName + ";" +
"Database='CeilInn1';" +
"Integrated Security=Yes"))
{
SqlCommand cmdCustomers =
new SqlCommand("SELECT * FROM Hotel.Rooms " +
"WHERE RoomNumber = '" + txtRoomNumber.Text + "';",
cntCeilInn);
SqlDataAdapter sdaRooms = new SqlDataAdapter();
DataSet dsRooms = new DataSet("Rooms");
cntCeilInn.Open();
sdaRooms.SelectCommand = cmdCustomers;
sdaRooms.Fill(dsRooms);
foreach (DataRow drRoom in dsRooms.Tables[0].Rows)
txtRoomDetails.Text = "Room Type: " + drRoom["RoomType"].ToString() + Environment.NewLine +
"Bed Type: " + drRoom["BedType"].ToString() + Environment.NewLine +
"Rate: " + drRoom["Rate"].ToString();
}
}
- On the main menu, click Project -> Add Windows Form...
- Set the name to RoomsAssignments
- Click Add
- Design the form as follows:
|
Control |
(Name) |
Text |
Anchor |
DataGriView |
|
dgvRegistrations |
|
Top, Bottom, Left, Right |
Button |
|
btnRoomAssignment |
New Room Assignment ... |
Bottom, Right |
Button |
|
btnClose |
Close |
Bottom, Right |
|
- Double-click an unoccupied area of the form
- Implement the event as follows:
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.SqlClient;
namespace CeilInn1
{
public partial class RoomsAssignments : Form
{
public RoomsAssignments()
{
InitializeComponent();
}
private void ShowRoomsAssignments()
{
// Specify the name of your server. Ours is named EXPRESSION
string strServerName = "EXPRESSION";
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source='" + strServerName + "';" +
"Database='CeilInn1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdAssignments =
new SqlCommand("SELECT AssignmentNumber, ProcessedBy, DateOccupied, " +
" ProcessedFor, RoomNumber, RateApplied, PhoneCharge " +
"FROM Room.Assignments; ",
cntCeilInn);
SqlDataAdapter sdaAssignments = new SqlDataAdapter();
DataSet dsAssignments = new DataSet("Assignments");
cntCeilInn.Open();
sdaAssignments.SelectCommand = cmdAssignments;
sdaAssignments.Fill(dsAssignments);
dgvAssignments.DataSource = dsAssignments.Tables[0];
}
}
private void RoomsAssignments_Load(object sender, EventArgs e)
{
ShowRoomsAssignments();
}
}
}
- Return to the form and double-click the New Room Assignment button
- Implement the event as follows:
private void btnRoomAssingment_Click(object sender, EventArgs e)
{
string strServerName = "EXPRESSION";
RoomAssignment ra = new RoomAssignment();
if (ra.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrEmpty(ra.txtEmployeeNumber.Text))
{
MessageBox.Show("You must specify the employee who processed the transaction.",
"Ceil Inn Hotel - Rooms Assignments",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (string.IsNullOrEmpty(ra.txtAccountNumber.Text))
{
MessageBox.Show("You must specify the customer whose transaction was processed.",
"Ceil Inn Hotel - Rooms Assignments",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (string.IsNullOrEmpty(ra.txtRoomNumber.Text))
{
MessageBox.Show("You must specify the room number that was processed.",
"Ceil Inn Hotel - Rooms Assignments",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (string.IsNullOrEmpty(ra.txtRateApplied.Text))
{
MessageBox.Show("You must specify the rate that was applied.",
"Ceil Inn Hotel - Rooms Assignments",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using (SqlConnection cntCeilInn =
new SqlConnection("Data Source='" + strServerName + "';" +
"Database=CeilInn1;" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdCeilInn =
new SqlCommand("INSERT INTO Room.Assignments( " +
"ProcessedBy, DateOccupied, ProcessedFor, " +
"RoomNumber, RateApplied, Notes) " +
"VALUES('" + ra.txtEmployeeNumber.Text + "', '" +
ra.dtpDateProcessed.Value + "', '" + ra.txtAccountNumber.Text +
"', '" + ra.txtRoomNumber.Text + "', " + double.Parse(ra.txtRateApplied.Text) + ", " +
ra.txtPhoneCharge.Text + ", '" + ra.txtNotes.Text + "');",
cntCeilInn);
cntCeilInn.Open();
cmdCeilInn.ExecuteNonQuery();
MessageBox.Show("A new Room has been built.",
"Ceil Inn Hotel - Customers",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
ShowRoomsAssignments();
}
- Return to the form and double-click the Close button
- Implement the event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
|
|