public IDataCommand CreateCommand(IDataCommandTemplate commandTemplate, DataParameterValue parameterValue)
        {
            IDbCommand command = Connection().CreateCommand();

            command.Connection  = UnderlyingConnection();
            command.CommandType = commandTemplate.CommandType;
            command.CommandText = commandTemplate.CommandText;

            foreach (IDataParameterTemplate parameterTemplate in commandTemplate.Parameters)
            {
                IDbDataParameter parameter = command.CreateParameter();
                parameter.ParameterName = parameterTemplate.Name;

                // AdomdParamater has not implemented the property DbType - awesome
                if (!parameter.GetType().Equals(typeof(AdomdParameter)))
                {
                    parameter.DbType = parameterTemplate.DbType;
                }

                parameter.Value = parameterValue(parameterTemplate);

                command.Parameters.Add(parameter);
            }

            return(new DataCommand(command));
        }
Exemple #2
0
    public static void SetValue <T>(this IDataParameter parameter, DataParameterValue <T> value)
    {
        Assert.IsNotNull(parameter, nameof(parameter));
        Assert.IsInRange(value.Type == DataParameterValueType.Value || value.Type == DataParameterValueType.Null ||
                         value.Type == DataParameterValueType.Default);

        object valueObject;

        switch (value.Type)
        {
        case DataParameterValueType.Value:
            valueObject = value.Value;
            break;

        case DataParameterValueType.Null:
            valueObject = DBNull.Value;
            break;

        case DataParameterValueType.Default:
            valueObject = null;
            break;

        default:
            throw new ArgumentException();
        }

        parameter.Value = valueObject;
    }
Exemple #3
0
        public static NonQueryResult ExecuteNonQuery(IDataConnection connection, IDataCommandTemplate commandTemplate, DataParameterValue parameterValue, bool collectPrimaryKey)
        {
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }

            using (IDataCommand command = connection.CreateCommand(commandTemplate, parameterValue))
            {
                using (IDbTransaction transaction = connection.BeginTransaction())
                {
                    command.Transaction = transaction;

                    int rowsAffected = command.ExecuteNonQuery();

                    if (rowsAffected != 1)
                    {
                        throw new DataException("Number of rows affected = " + rowsAffected + " expected 1");
                    }

                    object primaryKey;

                    if (collectPrimaryKey)
                    {
                        DataCommandTemplate identity = new DataCommandTemplate(CommandType.Text, "SELECT @@IDENTITY Id");

                        using (IDbCommand primaryKeyCommand = connection.CreateCommand(identity, parameterValue))
                        {
                            primaryKeyCommand.Transaction = transaction;

                            primaryKey = primaryKeyCommand.ExecuteScalar();
                        }
                    }
                    else
                    {
                        primaryKey = null;
                    }

                    transaction.Commit();

                    return(new NonQueryResult(primaryKey, rowsAffected));
                }
            }
        }