Click here to Skip to main content
15,896,456 members
Articles / Database Development / SQL Server / SQL Server 2008

Simple Data Access in C#

Rate me:
Please Sign up or sign in to vote.
4.11/5 (15 votes)
11 Jan 2009CPOL5 min read 83.9K   1.4K   58  
Fast and easy to use data access class library.
using System;
using System.Data;


namespace Yap.Data.Client
{
	/// <summary>
	/// Represents a parameter to a <see cref="Yap.Data.Client.Command"/>.
	/// </summary>
	[Serializable]
	public class Parameter : IEquatable<Parameter>
	{
		private readonly String _Name;
		private Object _Value;
		private readonly ParameterDirection _Direction = ParameterDirection.Input;
		private readonly Type _ParameterType = typeof(String);
		private readonly Int32? _Size;

		/// <summary>
		/// Gets the name of the parameter.
		/// </summary>
		public String Name
		{
			get { return _Name; }
		}

		/// <summary>
		/// Gets or sets the value of the parameter.
		/// </summary>
		public Object Value
		{
			get { return _Value; }
			set { _Value = value; }
		}

		/// <summary>
		/// Gets the direction of the parameter.
		/// </summary>
		public ParameterDirection Direction
		{
			get { return _Direction; }
		}

		/// <summary>
		/// Gets the type of the parameter.
		/// </summary>
		public Type ParameterType
		{
			get { return _ParameterType; }
		}

		/// <summary>
		/// Gets the size of the parameter.
		/// </summary>
		public Int32? Size
		{
			get { return _Size; }
		}

		/// <summary>
		/// Initializes a new instance of <see cref="Yap.Data.Client.Parameter"/> with specified name.
		/// </summary>
		/// <param name="name">The name of the parameter.</param>
		public Parameter(String name)
		{
			if (String.IsNullOrEmpty(name))
			{
				throw new ArgumentNullException("name");
			}
			_Name = name;
		}

		/// <summary>
		/// Initializes a new instance of <see cref="Yap.Data.Client.Parameter"/> with specified name and value.
		/// </summary>
		/// <param name="name">The name of the parameter.</param>
		/// <param name="value">The value of the parameter.</param>
		public Parameter(String name, Object value)
			: this(name)
		{
			_Value = value;
			if (value != null)
			{
				_ParameterType = _Value.GetType();
			}
		}

		/// <summary>
		/// Initializes a new instance of <see cref="Yap.Data.Client.Parameter"/> with specified name, value and type.
		/// </summary>
		/// <param name="name">The name of the parameter.</param>
		/// <param name="value">The value of the parameter.</param>
		/// <param name="type">The type of the parameter.</param>
		public Parameter(String name, Object value, Type type)
			: this(name, value)
		{
			_ParameterType = type;
		}

		/// <summary>
		/// Initializes a new instance of <see cref="Yap.Data.Client.Parameter"/> with specified name, value and direction.
		/// </summary>
		/// <param name="name">The name of the parameter.</param>
		/// <param name="value">The value of the parameter.</param>
		/// <param name="direction">The direction of the parameter.</param>
		public Parameter(String name, Object value, ParameterDirection direction)
			: this(name, value)
		{
			_Direction = direction;
		}

		/// <summary>
		/// Initializes a new instance of <see cref="Yap.Data.Client.Parameter"/> with specified name, value, type and direction.
		/// </summary>
		/// <param name="name">The name of the parameter.</param>
		/// <param name="value">The value of the parameter.</param>
		/// <param name="type">The type of the parameter.</param>
		/// <param name="direction">The direction of the parameter.</param>
		public Parameter(String name, Object value, Type type, ParameterDirection direction)
			: this(name, value, type)
		{
			_Direction = direction;
		}

		/// <summary>
		/// Initializes a new instance of <see cref="Yap.Data.Client.Parameter"/> with specified name, value, type, size and direction.
		/// </summary>
		/// <param name="name">The name of the parameter.</param>
		/// <param name="value">The value of the parameter.</param>
		/// <param name="type">The type of the parameter.</param>
		/// <param name="size">The size of the parameter.</param>
		/// <param name="direction">The direction of the parameter.</param>
		public Parameter(String name, Object value, Type type, Int32? size, ParameterDirection direction)
			: this(name, value, type, direction)
		{
			_Size = size;
		}
		
		/// <summary>
		/// Determines whether this instance and another specified <see cref="Yap.Data.Client.Parameter"/> object have the same value.
		/// </summary>
		/// <param name="other">Another <see cref="Yap.Data.Client.Parameter"/>.</param>
		/// <returns>true if the value of the other parameter is the same as this instance; otherwise, false.</returns>
		public Boolean Equals(Parameter other)
		{
			if (other == null)
			{
				return false;
			}
			return
				Equals(Name, other.Name) &&
				Equals(Value, other.Value) &&
				Equals(ParameterType, other.ParameterType) &&
				Equals(Size, other.Size) &&
				Equals(Direction, other.Direction);
		}

		/// <summary>
		/// Determines whether this instance of <see cref="Yap.Data.Client.Parameter"/> and a specified object, which must also be a <see cref="Yap.Data.Client.Parameter"/> object, have the same value.
		/// </summary>
		/// <param name="obj">An <see cref="System.Object"/>.</param>
		/// <returns>true if obj is a <see cref="Yap.Data.Client.Parameter"/> and its value is the same as this instance; otherwise, false.</returns>
		public override Boolean Equals(Object obj)
		{
			return Equals(obj as Parameter);
		}

		/// <summary>
		/// Returns the hash code for this parameter.
		/// </summary>
		/// <returns>The hash code for this parameter.</returns>
		public override Int32 GetHashCode()
		{
			return Name.GetHashCode();
		}

		/// <summary>
		/// Returns a string representation of the parameter.
		/// </summary>
		/// <returns>A string representation of the current parameter.</returns>
		public override String ToString()
		{
			return Name;
		}
	}
}

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 Code Project Open License (CPOL)


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

Comments and Discussions