Click here to Skip to main content
15,881,757 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.1K   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;

namespace DBHelper
{
   /// <summary>
   /// A collection of elements of type string
   /// </summary>
   public class SPCollection: System.Collections.CollectionBase
   {
      /// <summary>
      /// Initializes a new empty instance of the SPCollection class.
      /// </summary>
      public SPCollection()
      {
         // empty
      }

      /// <summary>
      /// Initializes a new instance of the SPCollection class, containing elements
      /// copied from an array.
      /// </summary>
      /// <param name="items">
      /// The array whose elements are to be added to the new SPCollection.
      /// </param>
      public SPCollection(string[] items)
      {
         this.AddRange(items);
      }

      /// <summary>
      /// Initializes a new instance of the SPCollection class, containing elements
      /// copied from another instance of SPCollection
      /// </summary>
      /// <param name="items">
      /// The SPCollection whose elements are to be added to the new SPCollection.
      /// </param>
      public SPCollection(SPCollection items)
      {
         this.AddRange(items);
      }

      /// <summary>
      /// Adds the elements of an array to the end of this SPCollection.
      /// </summary>
      /// <param name="items">
      /// The array whose elements are to be added to the end of this SPCollection.
      /// </param>
      public virtual void AddRange(string[] items)
      {
         foreach (string item in items)
         {
            this.List.Add(item);
         }
      }

      /// <summary>
      /// Adds the elements of another SPCollection to the end of this SPCollection.
      /// </summary>
      /// <param name="items">
      /// The SPCollection whose elements are to be added to the end of this SPCollection.
      /// </param>
      public virtual void AddRange(SPCollection items)
      {
         foreach (string item in items)
         {
            this.List.Add(item);
         }
      }

      /// <summary>
      /// Adds an instance of type string to the end of this SPCollection.
      /// </summary>
      /// <param name="value">
      /// The string to be added to the end of this SPCollection.
      /// </param>
      public virtual void Add(string value)
      {
         this.List.Add(value);
      }

      /// <summary>
      /// Determines whether a specfic string value is in this SPCollection.
      /// </summary>
      /// <param name="value">
      /// The string value to locate in this SPCollection.
      /// </param>
      /// <returns>
      /// true if value is found in this SPCollection;
      /// false otherwise.
      /// </returns>
      public virtual bool Contains(string value)
      {
         return this.List.Contains(value);
      }

      /// <summary>
      /// Return the zero-based index of the first occurrence of a specific value
      /// in this SPCollection
      /// </summary>
      /// <param name="value">
      /// The string value to locate in the SPCollection.
      /// </param>
      /// <returns>
      /// The zero-based index of the first occurrence of the _ELEMENT value if found;
      /// -1 otherwise.
      /// </returns>
      public virtual int IndexOf(string value)
      {
         return this.List.IndexOf(value);
      }

      /// <summary>
      /// Inserts an element into the SPCollection at the specified index
      /// </summary>
      /// <param name="index">
      /// The index at which the string is to be inserted.
      /// </param>
      /// <param name="value">
      /// The string to insert.
      /// </param>
      public virtual void Insert(int index, string value)
      {
         this.List.Insert(index, value);
      }

      /// <summary>
      /// Gets or sets the string at the given index in this SPCollection.
      /// </summary>
      public virtual string this[int index]
      {
         get
         {
            return (string) this.List[index];
         }
         set
         {
            this.List[index] = value;
         }
      }

      /// <summary>
      /// Removes the first occurrence of a specific string from this SPCollection.
      /// </summary>
      /// <param name="value">
      /// The string value to remove from this SPCollection.
      /// </param>
      public virtual void Remove(string value)
      {
         this.List.Remove(value);
      }

      /// <summary>
      /// Type-specific enumeration class, used by SPCollection.GetEnumerator.
      /// </summary>
      public class Enumerator: System.Collections.IEnumerator
      {
         private System.Collections.IEnumerator wrapped;

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

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

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

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

         public void Reset()
         {
            this.wrapped.Reset();
         }
      }

      /// <summary>
      /// Returns an enumerator that can iterate through the elements of this SPCollection.
      /// </summary>
      /// <returns>
      /// An object that implements System.Collections.IEnumerator.
      /// </returns>        
      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