Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Creating a reuseable SqlParameter object

Part of the sweetness in working with the .Text source is learning more about .NET from ScottW.  Here is how he creates a SQL Parameter object which is reused by multiple methods, usually as part of the SqlParameter array passed to SqlHelper.  I hadn't thought of creating a reusable SQLParameter object like this before. 

In .Text, a staple SQL Parameter is the BlogID. First create the object.

private SqlParameter BlogIDParam
{
 get
 {
  return  SqlHelper.MakeInParam("@BlogID",SqlDbType.Int,4,Config.CurrentBlog().BlogID);
 }
}

Now that its available, just add it in the SQL Parameter array list (in bold).   Smart!

public int InsertCategory(LinkCategory lc)
{
 SqlParameter[] p =
 {

  SqlHelper.MakeInParam("@Title",SqlDbType.NVarChar,150,lc.Title),
  SqlHelper.MakeInParam("@Active",SqlDbType.Bit,1,lc.IsActive),
  SqlHelper.MakeInParam("@CategoryType",SqlDbType.TinyInt,1,lc.CategoryType),
  SqlHelper.MakeInParam("@Description",SqlDbType.NVarChar,1000,DataHelper.CheckNull(lc.Description)),
  BlogIDParam,
  SqlHelper.MakeInParam("@TermID",SqlDbType.Int,4,lc.TermID),
  SqlHelper.MakeOutParam("@CategoryID",SqlDbType.Int,4)
 };
 NonQueryInt("blog_InsertCategory",p);
 return (int)p[6].Value;
}

Comments (0) | Post RSS RSS comment feed

Posted on 1/3/2005 9:00:00 PM by Dave Burke
Categories: .NET
Tags:

Related posts


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke