
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;


namespace STMes.DataProvider
{
	/// <summary>
	/// Implements access to the Data Provider for SQL Server.
	/// </summary>
	/// <remarks>
	/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
	/// </remarks>
	/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
	public class SqlDataProvider: IDataProvider
	{
		/// <summary>
		/// Creates the database connection object.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
		/// <returns>The database connection object.</returns>
		IDbConnection IDataProvider.CreateConnectionObject()
		{
			return new SqlConnection();
		}
		IDbConnection IDataProvider.CreateConnectionObject(string connectionString )
		{
			SqlConnection conn = new SqlConnection(connectionString);
			
			return conn;
		}
		/// <summary>
		/// Creates the data adapter object.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
		/// <returns>A data adapter object.</returns>
		DbDataAdapter IDataProvider.CreateDataAdapterObject()
		{
			return new SqlDataAdapter();
		}

		/// <summary>
		/// Populates the specified IDbCommand object's Parameters collection with 
		/// parameter information for the stored procedure specified in the IDbCommand.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
		/// <param name="command">The IDbCommand referencing the stored procedure for which the parameter information is to be derived. The derived parameters will be populated into the Parameters of this command.</param>
		void IDataProvider.DeriveParameters(IDbCommand command)
		{
			SqlCommandBuilder.DeriveParameters((SqlCommand)command);
		}

		/// <summary>
		/// Returns connection type.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
		/// <value>An instance of the <see cref="Type"/> class.</value>
		public Type ConnectionType
		{
			get
			{
				return typeof(SqlConnection);
			}
		}

		/// <summary>
		/// Returns the data provider name.
		/// </summary>
		/// <remarks>
		/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
		/// </remarks>
		/// <seealso cref="DbManager.AddDataProvider">AddDataProvider Method</seealso>
		/// <value>Data provider name.</value>
		public string Name
		{
			get 
			{
				return "Sql";
			}
		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public IDbDataParameter CreateDataParameter()
		{
			// TODO:  Ìí¼Ó SqlDataProvider.CreateDataParameter ÊµÏÖ
			return new SqlParameter();
		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <param name="da"></param>
		/// <returns></returns>
		public Object CreateCommandBuilder(DbDataAdapter da)
		{
			return new SqlCommandBuilder(da as SqlDataAdapter) as object;
		}
	
		/// <summary>
		/// Test Connection Info(SQL SERVER)
		/// </summary>
		/// <param name="db">Test Connection Object</param> 
		/// <returns>Test Result</returns>
		public bool Ping(ref IDbConnection db)
		{

			SqlDataReader read = null;
			try
			{
				using(SqlCommand cmd = new SqlCommand("SELECT 1 FROM DUAL",(SqlConnection)db))
				{
					read = cmd.ExecuteReader();
				}
				return true;
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex.Message); 
			}
			finally
			{
				if(read!=null) read.Close();
			}
			return false;
		}

        public int GetSID(ref IDbConnection db)
        {
            return -1;
        }
	}
	
}
