The 7th page of the OfficeTest General Tests tutorial looks at using select stored procedures. If you want to review the other tutorials, the central page can be found here Firebird and .NET 2.0 development Example.
7. Select Stored Procedures.
This is a very powerfull feature because the stored procedure ends up working like a view that supports an entire programming langauge.
The example web page is very simple. It provides a dropdown list containing all the possible JobPositions. You can select any single JobPosition and then the GridView below it is filled with the results of all Employees for that JobPosition. If the checkbox is checked then the stored procedure is called directly and all columns are returned. If the checkbox is not checked then a select from the stored procedure is called which selects only the firstname.
Employee: Stored Procedure/Trigger
I won't spend any time on the HTML since it's very straight forward and not the focus of this tutorial.
GetEmployeesByJob
CREATE PROCEDURE "GetEmployeesByJob" ( "JobPositionID" INTEGER )
RETURNS ( "EmployeeID" INTEGER, "FirstName" VARCHAR(50), "LastName" VARCHAR(50) )
AS
BEGIN
FOR select "EmployeeID", "FirstName", "LastName"
from "Employee"
where "JobPositionID" = :"JobPositionID"
into :"EmployeeID", :"FirstName", :"LastName" DO
SUSPEND;
END
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {
string connectionString = WebConfigurationManager.ConnectionStrings["OfficeTestFirebird"].ConnectionString;
FbConnection con = new FbConnection(connectionString);
try {
con.Open();
FbCommand cmd;
if (cbGetAllColumns.Checked) {
cmd = new FbCommand("\"GetEmployeesByJob\"", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add("@JobPositionID", Int32.Parse(lstJob.SelectedValue));
} else {
cmd = new FbCommand("SELECT \"FirstName\" FROM \"GetEmployeesByJob\"(@JobPositionID)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
cmd.Parameters.Add("@JobPositionID", Int32.Parse(lstJob.SelectedValue));
}
FbDataReader reader = cmd.ExecuteReader();
try {
gvEmployees.DataSource = reader;
gvEmployees.DataBind();
} finally {
reader.Close();
}
} finally {
con.Close();
}
}
Stored Procedure
cmd = new FbCommand("\"GetEmployeesByJob\"", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add("@JobPositionID", Int32.Parse(lstJob.SelectedValue));
Selecting from a procedure
cmd = new FbCommand("SELECT \"FirstName\" FROM \"GetEmployeesByJob\"(@JobPositionID)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
cmd.Parameters.Add("@JobPositionID", Int32.Parse(lstJob.SelectedValue));
Well thats really all there is to it. There are times when putting additional logic in the stored procedure will provide better performance by reducing the data returned to the calling application. However the application may still want to tailor the information it receives. This mechanism provides an elegant solution to that problem.
See next Tutorial...Stored Procedure web page which calls a stored procedure with no result set and retrieves a computed value.