Fundamentals of Stored Procedures



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 LearningPractical Learning: Introducing Procedures

  1. Start Microsoft Visual C#
  2. In Server Explorer, expand the server that contains your installation of SQL Server
  3. Right-click the name of your server and click New Database
  4. Set the name of the database to SuperMarket and click OK
  5. In Server Explorer, expand the SuperMarket database. Right-click the Tables node and click New Table
  6. Create a new table as follows:
  7. Save the table as Employees and close it
  8. In Server Explorer, expand the Tables node under SuperMarket and double-click Employees
  9. Fill it up with a few records as follows:
  10. Close the table
  11. Start a new Windows Application named SuperMarket1
  12. In Server Explorer, under the Tables node of the SuperMarket database, drag Employees and drop it on the form
  13. On the main menu, click Data -> Generate Dataset...
  14. Change the name of the dataset to dsEmployees and click OK
  15. 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
  16. Double-click the Load button
  17. Return to the form and double-click the Close button
  18. Implement the events as follows:
    private void Form1_Load(object sender, System::EventArgs  e)
    private void btnClose_Click(object sender, System.EventArgs e)
  19. Test the application:
  20. Close the form to return to Visual C#



Home Copyright © 2004-2006 FunctionX, Inc. Next