[dba-VB] C# inheritance - first foray

jwcolby jwcolby at colbyconsulting.com
Fri Mar 12 22:51:00 CST 2010


Tonight I made my first foray into doing my own base class and inheriting that, as opposed to 
inheriting a base class from the framework, which I have done many times.  I had designed a class 
which implemented my stored procedure widget.  Basically the what and why was that I wanted to 
absolutely standardize the names and data types of a set of parameters that I pass to my stored 
procedures in SQL Server.

I developed a fair number of SPs over a couple of years. I didn't check back (for naming 
conventions) to previous SPs as I developed the next so I ended up with the "same" variable named 
many different things.  @FieldName and @FldName, @TableName and @tblName etc.

Once I started trying to drive them from C# is became a PITA because the name passed in from C# must 
exactly match the name in the SP definition line.

Thus I started defining a standard out in my C# code and rebuilding the SPs as I encountered errors 
trying to run the SPs.

Code like:

public void paramDBName(string lstrDBName)
{
	sCmd.Parameters.Add(new SqlParameter( "@DBName", SqlDbType.VarChar, 50));
	sCmd.Parameters["@DBName"].Value = lstrDBName;
}

and

public void paramErrorDesc()
{
     base.pCmd.Parameters.Add(new SqlParameter("@ErrorDesc", System.Data.SqlDbType.VarChar, 4000));
     base.pCmd.Parameters["@ErrorDesc"].Direction = System.Data.ParameterDirection.Output;
}

This effectively "hard codes" the parameter name, but allows easy passing of the value going into 
that parameter, or defining the direction if it is a value coming back.

I have about 20 of these parameters.

So fine I did that.  Then I defined the command object, initialization code, variables to allow me 
to capture the time required to execute the SP, code to log the fact that the sp executed etc.  This 
worked great for my "generic" SPs.

Except that I eventually wanted to define some specific parameters for stored procedures used for 
purposed processes, such as backup / restore.  But I still wanted the code surrounding the command 
object to be standard.

So tonight I started carving that "class header" kind of stuff out into a base class, and inheriting 
that in the child classes.  It is interesting what you have to go through to make it happen.

I seem to have the main class that I carved apart working, now I have to go into the other classes 
and carve out the header stuff and have them inherit the base class.  It is also interesting working 
with projects within a solution.  The main clsStoredProcedure is out in its own project under the 
solution and has to be added under references etc.

Learning C# has been a journey, and I haven't gotten all that far along the road.

-- 
John W. Colby
www.ColbyConsulting.com



More information about the dba-VB mailing list