locked
DBCommandWrapper For Enterprise Library 2006 RRS feed

  • Question

  • User485225670 posted

    Hi,

    Since Enterprise Library 2006 do not support DBCommandWrapper, any body know how to convert this code below so it will be able to use for Enterprise Library 2006?

    Original code
    http://pgfoundry.org/projects/npgsqldaab/


    using System;
    using System.Data;
    using Npgsql;

    namespace Microsoft.Practices.EnterpriseLibrary.Data.PostgreSQL
    {
        /// <summary>
        /// <para>Represents a SQL statement or stored procedure to execute against a PostgreSQL database.</para>
        /// </summary>  
        public class NpgsqlCommandWrapper : DBCommandWrapper
        {
            private NpgsqlCommand command;
            private int rowsAffected;
            private object[] parameterValues;
            private bool needsParameters = false;
            private char parameterToken;

     
            internal NpgsqlCommandWrapper(string commandText, CommandType commandType, char parameterToken)
            {
                this.parameterToken = parameterToken;
                this.command = CreateCommand(commandText, commandType);
            }

      
            internal NpgsqlCommandWrapper(string commandText, CommandType commandType, char parameterToken, object[] parameterValues) : this(commandText, commandType, parameterToken)
            {
                this.parameterValues = parameterValues;
                if (commandType == CommandType.StoredProcedure)
                {
                    this.needsParameters = true;
                }
            }

         
            public override IDbCommand Command
            {
                get { return this.command; }
            }

         
            public override int RowsAffected
            {
                get { return this.rowsAffected; }
                set { this.rowsAffected = value; }
            }

         
            public override int CommandTimeout
            {
                get { return this.command.CommandTimeout; }
                set { this.command.CommandTimeout = value; }
            }

         
            public override void AddParameter(string name, DbType dbType, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
            {
                this.command.Parameters.Add(CreateParameter(name, dbType, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, value));
            }

          
            public void AddParameter(string name, NpgsqlTypes.NpgsqlDbType sqlType, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
            {
                NpgsqlParameter param = CreateParameter(name, DbType.String, size, direction, nullable, precision, scale, sourceColumn, sourceVersion, value);
                param.NpgsqlDbType = sqlType;
                this.command.Parameters.Add(param);
            }
            public override void AddParameter(string name, DbType dbType, ParameterDirection direction, string sourceColumn, DataRowVersion sourceVersion, object value)
            {
                NpgsqlParameter param = CreateParameter(name, dbType, 0, direction, false, 0, 0, sourceColumn, sourceVersion, value);
                this.command.Parameters.Add(param);
            }

         
            public override void AddOutParameter(string name, DbType dbType, int size)
            {
                AddParameter(name, dbType, size, ParameterDirection.Output, true, 0, 0, String.Empty, DataRowVersion.Default, DBNull.Value);
            }

           
            public override void AddInParameter(string name, DbType dbType)
            {
                AddParameter(name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, null);
            }

            public override void AddInParameter(string name, DbType dbType, object value)
            {
                AddParameter(name, dbType, ParameterDirection.Input, String.Empty, DataRowVersion.Default, value);
            }

        
            public override void AddInParameter(string name, DbType dbType, string sourceColumn, DataRowVersion sourceVersion)
            {
                AddParameter(name, dbType, 0, ParameterDirection.Input, true, 0, 0, sourceColumn, sourceVersion, null);
            }

         
            public override object GetParameterValue(string name)
            {
                return this.command.Parameters[BuildParameterName(name)].Value;
            }

        
            public override void SetParameterValue(string name, object value)
            {
                this.command.Parameters[BuildParameterName(name)].Value = (value == null) ? DBNull.Value : value;
            }

            public override void Dispose()
            {
                this.command.Dispose();
            }

          
            protected override void DoDiscoverParameters(char parameterToken)
            {
                this.parameterToken = parameterToken;
                using (NpgsqlCommand newCommand = CreateNewCommandAndConnectionForDiscovery())
                {
                    NpgsqlCommandBuilder.DeriveParameters(newCommand);

                    foreach (IDataParameter parameter in newCommand.Parameters)
                    {
                        IDataParameter cloneParameter = (IDataParameter)((ICloneable)parameter).Clone();
                        cloneParameter.ParameterName = BuildParameterName(cloneParameter.ParameterName);
                        this.command.Parameters.Add(cloneParameter);
                    }
                    newCommand.Connection.Close();
                }
            }

         
            protected override void DoAssignParameterValues()
            {
                if (SameNumberOfParametersAndValues() == false)
                {
    //                throw new InvalidOperationException(SR.ExceptionMessageParameterMatchFailure);
        throw new InvalidOperationException("The number of parameters does not match number of values for stored procedure.");
                }

                int returnParameter = 1;
                for (int i = 0; i < this.parameterValues.Length; i++)
                {
                    IDataParameter parameter = this.command.Parameters[i + returnParameter];

                    // There used to be code here that checked to see if the parameter was input or input/output
                    // before assigning the value to it. We took it out because of an operational bug with
                    // deriving parameters for a stored procedure. It turns out that output parameters are set
                    // to input/output after discovery, so any direction checking was unneeded. Should it ever
                    // be needed, it should go here, and check that a parameter is input or input/output before
                    // assigning a value to it.
                    SetParameterValue(parameter.ParameterName, this.parameterValues[i]);
                }
            }

            protected override bool DoIsFurtherPreparationNeeded()
            {
                return this.needsParameters;
            }

            private NpgsqlParameter CreateParameter(string name, DbType type, int size, ParameterDirection direction, bool nullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
            {
                NpgsqlParameter param = this.command.CreateParameter();
                param.ParameterName = BuildParameterName(name);
                param.DbType = type;
                param.Size = size;
                param.Direction = direction;
                param.IsNullable = nullable;
                param.Precision = precision;
                param.Scale = scale;
                param.SourceColumn = sourceColumn;
                param.SourceVersion = sourceVersion;
                param.Value = (value == null) ? DBNull.Value : value;

                return param;
            }

            private bool SameNumberOfParametersAndValues()
            {
                int returnParameterCount = 1;
                int numberOfParametersToStoredProcedure = this.command.Parameters.Count - returnParameterCount;
                int numberOfValuesProvidedForStoredProcedure = this.parameterValues.Length;
                return numberOfParametersToStoredProcedure == numberOfValuesProvidedForStoredProcedure;
            }

        
            private NpgsqlCommand CreateNewCommandAndConnectionForDiscovery()
            {
                NpgsqlConnection clonedConnection = (NpgsqlConnection)((ICloneable)this.command.Connection).Clone();
                clonedConnection.Open();
                NpgsqlCommand newCommand = CreateCommand(this.command.CommandText, this.command.CommandType);
                newCommand.Connection = clonedConnection;

                return newCommand;
            }

            private static NpgsqlCommand CreateCommand(string commandText, CommandType commandType)
            {
                NpgsqlCommand newCommand = new NpgsqlCommand();
                newCommand.CommandText = commandText;
                newCommand.CommandType = commandType;

                return newCommand;
            }

            private string BuildParameterName(string name)
            {
                //System.Diagnostics.Debug.Assert(parameterToken != 0x0000);
                if (name[0] != this.parameterToken)
                {
                    return name.Insert(0, new string(this.parameterToken, 1));
                }
                return name;
            }
        }
    }

    Monday, March 13, 2006 7:11 PM

All replies

  • User-658398405 posted

    u can use dbCommand  instead of DBCommandWrapper

    Tuesday, August 11, 2009 5:47 PM