
using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;

namespace STMes.DataProvider
{
	/// <summary>
	/// Implements access to the Data Provider for OLE DB.
	/// </summary>
	/// <remarks>
	/// See the <see cref="DbManager.AddDataProvider"/> method to find an example.
	/// </remarks>
	/// <seealso cref="DbManager.AddDataProvider">AddDataManager Method</seealso>
	public class OleDbDataProvider: 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 OleDbConnection();
		}
		IDbConnection IDataProvider.CreateConnectionObject(string connectionString )
		{
			OleDbConnection conn = new OleDbConnection(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 OleDbDataAdapter();
		}

		/// <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)
		{
			OleDbCommandBuilder.DeriveParameters((OleDbCommand)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(OleDbConnection);
			}
		}

		/// <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 "OleDb";
			}
		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public IDbDataParameter CreateDataParameter()
		{
			// TODO:  添加 OleDbDataProvider.CreateDataParameter 实现
			return new OleDbParameter();
		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <param name="da"></param>
		/// <returns></returns>
		public Object CreateCommandBuilder(DbDataAdapter da)
		{
			return new OleDbCommandBuilder(da as OleDbDataAdapter ) as object;
		}
	
		/// <summary>
		/// Test Connection Info(OLE DB)
		/// </summary>
		/// <param name="db">Test Connection Object</param> 
		/// <returns>Test Result</returns>
		public bool Ping(ref IDbConnection db)
		{
			// TODO:  添加 OleDbDataProvider.Ping 实现
			return true;
        }
        public int GetSID(ref IDbConnection db)
        {
            return -1;
        }
	}
}
