Imagine that your create a database that includes
employees. When you want to perform payroll for employees, you would
need their weekly hours and their hourly salaries. To calculate the
weekly salary, you would write an equation such as: Weekly Salary = Weekly Hours * Hourly Salary
Since there are various employees in the salary, you would need
a fast means of
performing this kind of assignment, maybe automatically, for each
employee. This is the basis
of a function. Referred to as a function or a routine
in most other programming languages, a procedure is an (or a relatively
small) assignment that can take care of a task in a database so that you
can call it as/when needed to get its result.
Practical
Learning: Introducing Procedures
|
|
- Start Microsoft Visual C#
- In Server Explorer, expand the server that contains your installation of
SQL Server
- Right-click the name of your server and click New Database
- Set the name of the database to SuperMarket and click OK
- In Server Explorer, expand the SuperMarket database. Right-click the
Tables node and click New Table
- Create a new table as follows:

- Save the table as Employees and close it
- In Server Explorer, expand the Tables node under SuperMarket and
double-click Employees
- Fill it up with a few records as follows:

- Close the table
- Start a new Windows Application named SuperMarket1
- In Server Explorer, under the Tables node of the SuperMarket database,
drag Employees and drop it on the form
- On the main menu, click Data -> Generate Dataset...
- Change the name of the dataset to dsEmployees and click OK

- Add a DataGrid control to the form as follows:
 |
Control |
Name |
Text |
Other Properties |
Form |
|
|
StartPosition: CenterScreen |
DataGrid |
|
|
Anchor: Top, Bottom, Left, Right
AutoFormat: Professional 3
DataSource: dsEmployees1.Employees |
Button |
btnLoad |
Load |
Anchor: Bottom, Right |
Button |
btnClose |
Close |
Anchor: Bottom, Left |
|
- Double-click the Load button
- Return to the form and double-click the Close button
- Implement the events as follows:
private void Form1_Load(object sender, System::EventArgs e)
{
this.sqlDataAdapter1.Fill(this.dsEmployees1);
}
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
|
- Test the application:
- Close the form to return to Visual C#
|
|
|