Example #1
0
        public int ExecuteNonQuery(string commandText, DbParameter[] parameters, CommandType commandType)
        {
            var rowsAffected = 0;

            using (var connection = SafelyOpenConnection())
            {
                var cmdExecute = _builder.BuildCommand(connection, commandText, parameters, commandType);

                try
                {
                    cmdExecute.CommandType = commandType;
                    rowsAffected           = cmdExecute.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                    throw e;
                }
                finally
                {
                    SafelyCloseConnection();
                }
            }

            return(rowsAffected);
        }
Example #2
0
 public static Task ExecuteAsync(this ICommandBuilder builder, IInstrument instrument)
 {
     return(instrument.CommandAsync(builder.BuildCommand()));
 }
Example #3
0
        public IEnumerable <TRow> Execute <TRow>(ISelect Query)
            where TRow : new()
        {
            DbCommand    command;
            DbDataReader reader;

            IColumn[] columns;
            TRow      row;
            Type      type, underlyingType;

            //pdcs=TypeDescriptor.GetProperties(typeof(T));

            columns = Query.Columns.ToArray();
            command = commandBuilder.BuildCommand(Query);

            using (DbConnection connection = connectionFactory.CreateConnection())
            {
                connection.Open();
                command.Connection = connection;
                PropertyDescriptorCollection pdcs;
                object value;

                pdcs = TypeDescriptor.GetProperties(typeof(TRow));
                // ensure that all properties exist
                for (int t = 0; t < columns.Length; t++)
                {
                    if (pdcs[columns[t].Name] == null)
                    {
                        throw new KeyNotFoundException($"Cannot find property {columns[t].Name} in descriptor collection");
                    }
                }

                try
                {
                    reader = command.ExecuteReader();
                }
                catch (Exception ex)
                {
                    throw new ORMException(command, ex);
                }


                using (reader)
                {
                    while (reader.Read())
                    {
                        row = new TRow();
                        for (int t = 0; t < columns.Length; t++)
                        {
                            value = reader[t];
                            // cannot convert DBNull to Nullable<>
                            if (value == DBNull.Value)
                            {
                                value = null;
                            }
                            else
                            {
                                type           = pdcs[columns[t].Name].PropertyType;
                                underlyingType = Nullable.GetUnderlyingType(type);
                                if (underlyingType != null)
                                {
                                    type = underlyingType;
                                }

                                if (type.IsEnum)
                                {
                                    value = Enum.ToObject(type, value);
                                }
                            }
                            pdcs[columns[t].Name].SetValue(row, value);
                        }
                        yield return(row);
                    }
                }
                connection.Close();
            }
        }