Tutorial 3: Generating Wrappers and Custom Classes For Existing Stored Procedures With Monstarillo

Introduction:

The purpose of this tutorial is to demonstrate the use of Monstarillo in a real world scenario. We will be generating code to access the stored procedures that are in the Northwind database.  As you will see, most of the code will be generated for you by Monstarillo and will take only a fraction of the time it would take to write yourself.

This tutorial should take about 30 minutes to complete.

There is an online  support forum dedicated to this case study.

Database setup:

We will be using the Northwind database for this tutorial.

Create a C# windows project and take note of its location.

Run Monstarillo and enter the database connection information.

Click the Generate Code using existing Stored Procedures button. 

Change the output path to the location of your new project.  We will be using YellowbridgeSoftwareInc as the base namespace for this tutorial.

Press the Generate Stored Procedure Wrappers, info classes and collections button.  Monstarillo will read the database and bring up a form showing you all of the stored procedures in the database you have selected.

 

Select the CustOrderHist stored procedure by clicking on it in the list.

Selecting a stored procedure will display a grid that will allow you to assign values to stored procedure parameters.  Enter ALFKI as the parameter for @CustomerID.  The value in the Class Name text box will become the class name.  The value in the Info Collection Name text box will become the name of the info class collection.  The value in the Info Class Name text box will become the name of the info class.  The info class is a custom class that will have properties for each of the columns in a database table.  The info class collection is a collection of info classes.  Clicking the Show Code in Window button will have Monstarillo bring up three windows displaying the three classes it will generate.  Clicking the Show files checkbox will have Monstarillo bring up Windows Explorer showing you the files it has generated.  Lets accept the defaults for this tutorial.

Click the Generate Code button.

 

Load your new C# application in Visual Studio.  Open the solution explorer and press the show all files button.  Select the Business, BusinessBase, Info, Persist, and PersistBase folders right click and select include in project.

Add an Application Configuration file to the project.

Next we will add an appSettings section to the app.config and add a ConnectionString key to it.  Modify the app.config file so that is looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="data source=(local);initial catalog=northwind ;integrated security=SSPI;persist security info=False;packet size=4096" />
</appSettings></configuration>

Add a datagrid to the Form1 form. 

Add a button to the Form1 form.  Double click on the button to add code to the button's click event.  Add the following code to the button's click event handler.

YellowbridgeSoftwareInc.Persist.CustOrderHist persist = new YellowbridgeSoftwareInc.Persist.CustOrderHist( System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString() );
dataGrid1.DataSource = persist.CustOrderHist_Info( "ALFKI");

Notice that you have passed a database connection string to the constructor of the YellowbridgeSoftwareInc.Persist.CustOrderHist object.  Next you called the YellowbridgeSoftwareInc.Persist.CustOrderHist object's CustOrderHist_Info method passing the value of "ALFKI" as a parameter.  If you have called the CustOrderHist_DataTable method a DataTable would have been returned instead of the custom collection.

Run the application and press the button.  Notice that the grid is filled with data from the CustOrderHist stored procedure.

We have seen how easy it is to create wrappers for existing stored procedures using Monstarillo.  We have seen how simple it is to use the code generated by Monstarillo. 

For more information on Monstarillo visit http://www.yellow-bridge.com .

Discuss Monstarillo with others at http://forums.yellow-bridge.com .