using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml.Serialization;

namespace VoServices.Footprint.Web
{
	/// <summary>
	/// Summary description for BaseObject.
	/// </summary>
	public class BaseObject
	{
		protected SqlConnection _databaseConnection = null;
		protected SqlTransaction _databaseTransaction = null;

		public BaseObject()
		{
		}

		public BaseObject(SqlConnection databaseConnection)
		{
			_databaseConnection = databaseConnection;
		}

		public SqlConnection DatabaseConnection
		{
			get
			{
				try
				{
					if (_databaseConnection == null)
					{
						_databaseConnection = new SqlConnection(ConfigurationSettings.AppSettings["SqlConnectionString"]);
						_databaseConnection.Open();
					}

					return _databaseConnection;
				}
				catch
				{
					return null;
				}
				
			}
		}

		public SqlTransaction DatabaseTransaction
		{
			get
			{
				return _databaseTransaction;
			}
			set
			{
				_databaseTransaction = value;
			}
		}

		protected DataSet ExecuteCommand(SqlCommand cmd)
		{
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();

			da.Fill(ds);

			da.Dispose();
			cmd.Dispose();

			return ds;
		}

		protected DataSet ExecuteCommand(SqlCommand cmd, int startRecord, int maxRecords, out int found, string srcTable)
		{
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();

			found = da.Fill(ds, startRecord, maxRecords, srcTable);
			da.Dispose();

			return ds;
		}

		protected void ExecuteCommandNonquery(SqlCommand cmd)
		{
			cmd.ExecuteNonQuery();
		}
	}
}
