[dba-VB] Calling SP with params from C# sample

jwcolby jwcolby at colbyconsulting.com
Sat Nov 28 16:29:17 CST 2009


Shamil,

I ended up creating a "stored procedure class".  This class hosts a sCmd command object that can be 
programmed with the name of the SP, connection etc  but then included wrapping the lines of code to 
create specific often used parameters into functions.

         /// <summary>
         /// The following code creates standard parameters used all of the time in my stored procedures
         /// Basically just wrapper code so that I can call a function to create specific parameters 
as needed
         ///
         /// It also allows me to completely standardize the names, data type and length of 
parameters in the SPs etc.
         /// </summary>
         /// <param name="lstrTblName"></param>
         public void paramDBName(string lstrDBName)
         {
             sCmd.Parameters.Add(new SqlParameter( "@DBName", SqlDbType.VarChar, 50));
             sCmd.Parameters["@DBName"].Value = lstrDBName;
         }
         public void paramTblName(string lstrTblName)
         {
             sCmd.Parameters.Add(new SqlParameter("@TblName", SqlDbType.VarChar, 50));
             sCmd.Parameters["@TblName"].Value = lstrTblName;
         }
         public void paramErrorDesc()
         {
             sCmd.Parameters.Add(new SqlParameter("@ErrorDesc", System.Data.SqlDbType.VarChar, 4000));
             sCmd.Parameters["@ErrorDesc"].Direction = System.Data.ParameterDirection.Output;
         }

Then I can do things like:

             //
             //sp_AZOut_AZExportToOrdered_Append
             //
             sp = new StoredProcedure(myConnection, 
"_aDataMaster.dbo.sp_AZOut_AZExportToOrdered_Append");
             sp.paramDBName(lstrDBName);   //@DBName varchar(50) input
             sp.paramTblName(lstrTblName);   //@TblName varchar(50) input
             sp.paramErrorDesc();            //@ErrorDesc varchar(100) output
             sp.paramErrorNo();              //@ErrorNo int output,
             sp.paramReturnValue();          //RETURN
             retValue = sp.ExecuteSP();

I believe I am on my way now.

Thanks again,

John W. Colby
www.ColbyConsulting.com


Shamil Salakhetdinov wrote:
> John --
> 
> Tons of code for C# (or VB.NET) is not an issue at all. And for C#/VB.Net
> programmers also. You'll get adapted to that reality soon, I believe.



More information about the dba-VB mailing list