Click here to Skip to main content
15,885,895 members
Articles / Programming Languages / Visual Basic

SQL Stored Procedure Wrapper & Typed DataSet Generator for .NET

Rate me:
Please Sign up or sign in to vote.
4.50/5 (46 votes)
7 Dec 2002BSD4 min read 380.4K   6.1K   142  
This a small tool that will generate static methods in a class that acts as wrapper for SQL stored procedures. It either outputs a source file or a compiled assembly. Also supports automatic DataSet generation.
using System;
using System.Data;
using System.Data.SqlClient;

namespace DBHelper
{
#if(truenot)
   public class ParameterCollection: System.Collections.CollectionBase
   {
      SqlParameterCollection coll;

      public ParameterCollection(SqlParameterCollection coll)
      {
         this.coll = coll;
      }

      public new int Count 
      {
         get {return coll.Count;}
      }

      public virtual Parameter this[int index]
      {
         get
         {
            return new Parameter(coll[index]);
         }
         set
         {
            coll[index] = value.SqlParameter;
         }
      }
   }
#endif

   public class SPCollection: System.Collections.CollectionBase
   {
      public SPCollection()
      {
         // empty
      }

//      public SPCollection Create 
//      {
//         get 
//         {
//            SPCollection output = new SPCollection();
//            foreach (StoredProc sp in this)
//            {
//               if (sp.Create) output.Add(sp);
//            }
//            return output;
//         }
//      }
//
//      public SPCollection CreateStrong
//      {
//         get 
//         {
//            SPCollection output = new SPCollection();
//            foreach (StoredProc sp in this)
//            {
//               if (sp.CreateStrong) output.Add(sp);
//            }
//            return output;
//         }
//      }

      public SPCollection(StoredProc[] items)
      {
         this.AddRange(items);
      }

      public SPCollection(SPCollection items)
      {
         this.AddRange(items);
      }

      public virtual void AddRange(StoredProc[] items)
      {
         foreach (StoredProc item in items)
         {
            this.List.Add(item);
         }
      }

      public virtual void AddRange(SPCollection items)
      {
         foreach (StoredProc item in items)
         {
            this.List.Add(item);
         }
      }

      public void Add(string name)
      {
         //quickfix to "filter" source safe stuff
         if (!name.StartsWith("dt_"))
            Add(new StoredProc(name));
      }

      public virtual void Add(StoredProc value)
      {
         //if ((value.Name.ToLower().IndexOf("get") > -1) |
         //   (value.Name.ToLower().IndexOf("select") > -1) )
         //   value.CreateStrong = true;
         this.List.Add(value);
      }

      public bool Contains(string name)
      {
         foreach (StoredProc item in this)
         {
            if (item.Name == name) return true;
         }
         return false;
      }

      public virtual bool Contains(StoredProc value)
      {
         return this.List.Contains(value);
      }

      public virtual int IndexOf(StoredProc value)
      {
         return this.List.IndexOf(value);
      }

      public virtual void Insert(int index, StoredProc value)
      {
         this.List.Insert(index, value);
      }

      public virtual StoredProc this[int index]
      {
         get
         {
            return (StoredProc) this.List[index];
         }
         set
         {
            this.List[index] = value;
         }
      }

      public StoredProc this[string name]
      {
         get 
         {
            foreach(StoredProc sp in this)
            {
               if (sp.Name == name) return sp;
            }
            return null;
         }
      }

      public virtual void Remove(StoredProc value)
      {
         this.List.Remove(value);
      }

      public class Enumerator: System.Collections.IEnumerator
      {
         private System.Collections.IEnumerator wrapped;

         public Enumerator(SPCollection collection)
         {
            this.wrapped = ((System.Collections.CollectionBase)collection).GetEnumerator();
         }

         public StoredProc Current
         {
            get
            {
               return (StoredProc) (this.wrapped.Current);
            }
         }

         object System.Collections.IEnumerator.Current
         {
            get
            {
               return (StoredProc) (this.wrapped.Current);
            }
         }

         public bool MoveNext()
         {
            return this.wrapped.MoveNext();
         }

         public void Reset()
         {
            this.wrapped.Reset();
         }
      }
        
      public new virtual SPCollection.Enumerator GetEnumerator()
      {
         return new SPCollection.Enumerator(this);
      }
   }

   /// <summary>
   /// A dictionary with keys of type SqlDbType and values of type Type
   /// </summary>
   public class SqlDbTypeToTypeAssociation: System.Collections.DictionaryBase
   {
      /// <summary>
      /// Initializes a new empty instance of the SqlDbTypeToTypeAssociation class
      /// </summary>
      public SqlDbTypeToTypeAssociation()
      {
         // empty
      }

      /// <summary>
      /// Gets or sets the Type associated with the given SqlDbType
      /// </summary>
      /// <param name="key">
      /// The SqlDbType whose value to get or set.
      /// </param>
      public virtual Type this[SqlDbType key]
      {
         get
         {
            return (Type) this.Dictionary[key];
         }
         set
         {
            this.Dictionary[key] = value;
         }
      }

      /// <summary>
      /// Adds an element with the specified key and value to this SqlDbTypeToTypeAssociation.
      /// </summary>
      /// <param name="key">
      /// The SqlDbType key of the element to add.
      /// </param>
      /// <param name="value">
      /// The Type value of the element to add.
      /// </param>
      public virtual void Add(SqlDbType key, Type value)
      {
         this.Dictionary.Add(key, value);
      }

      /// <summary>
      /// Determines whether this SqlDbTypeToTypeAssociation contains a specific key.
      /// </summary>
      /// <param name="key">
      /// The SqlDbType key to locate in this SqlDbTypeToTypeAssociation.
      /// </param>
      /// <returns>
      /// true if this SqlDbTypeToTypeAssociation contains an element with the specified key;
      /// otherwise, false.
      /// </returns>
      public virtual bool Contains(SqlDbType key)
      {
         return this.Dictionary.Contains(key);
      }

      /// <summary>
      /// Determines whether this SqlDbTypeToTypeAssociation contains a specific key.
      /// </summary>
      /// <param name="key">
      /// The SqlDbType key to locate in this SqlDbTypeToTypeAssociation.
      /// </param>
      /// <returns>
      /// true if this SqlDbTypeToTypeAssociation contains an element with the specified key;
      /// otherwise, false.
      /// </returns>
      public virtual bool ContainsKey(SqlDbType key)
      {
         return this.Dictionary.Contains(key);
      }

      /// <summary>
      /// Determines whether this SqlDbTypeToTypeAssociation contains a specific value.
      /// </summary>
      /// <param name="value">
      /// The Type value to locate in this SqlDbTypeToTypeAssociation.
      /// </param>
      /// <returns>
      /// true if this SqlDbTypeToTypeAssociation contains an element with the specified value;
      /// otherwise, false.
      /// </returns>
      public virtual bool ContainsValue(Type value)
      {
         foreach (Type item in this.Dictionary.Values)
         {
            if (item == value)
               return true;
         }
         return false;
      }

      /// <summary>
      /// Removes the element with the specified key from this SqlDbTypeToTypeAssociation.
      /// </summary>
      /// <param name="key">
      /// The SqlDbType key of the element to remove.
      /// </param>
      public virtual void Remove(SqlDbType key)
      {
         this.Dictionary.Remove(key);
      }

      /// <summary>
      /// Gets a collection containing the keys in this SqlDbTypeToTypeAssociation.
      /// </summary>
      public virtual System.Collections.ICollection Keys
      {
         get
         {
            return this.Dictionary.Keys;
         }
      }

      /// <summary>
      /// Gets a collection containing the values in this SqlDbTypeToTypeAssociation.
      /// </summary>
      public virtual System.Collections.ICollection Values
      {
         get
         {
            return this.Dictionary.Values;
         }
      }
   }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions