private IList <T> ExecuteReader(IEnumerable <RowDataGatewayBinding> bindings, string commandText) { ActiveConnection activeConnection = new ActiveConnection(DatabaseName); using (DbConnection connection = activeConnection.CreateConnection()) { if (connection.State == ConnectionState.Closed) { connection.Open(); } using (DbCommand command = connection.CreateCommand()) { command.Connection = connection; command.CommandText = commandText; Bind(bindings, command); using (DbDataReader reader = command.ExecuteReader()) { List <T> items = new List <T>(); while (reader.Read()) { items.Add(Marshall(reader)); } return(items); } } } }
private void Execute(T item, string commandText, bool bindPrimaryKey) { ActiveConnection activeConnection = new ActiveConnection(DatabaseName); using (DbConnection connection = activeConnection.CreateConnection()) { if (connection.State == ConnectionState.Closed) { connection.Open(); } using (DbCommand command = connection.CreateCommand()) { using (DbTransaction transaction = connection.BeginTransaction()) { command.Connection = connection; command.Transaction = transaction; command.CommandText = commandText; Bind(item, command); if (bindPrimaryKey) { BindPrimaryKey(item, command); } int rowsAffected = command.ExecuteNonQuery(); if (rowsAffected != 1) { throw new ActiveRecordException(SR.GetString(SR.RowDataGateway_Bad_Update_Count, new object[] { rowsAffected, 1 })); } if (!bindPrimaryKey) { using (DbCommand primaryKeyCommand = connection.CreateCommand()) { primaryKeyCommand.Connection = connection; primaryKeyCommand.Transaction = transaction; primaryKeyCommand.CommandText = "SELECT @@IDENTITY Id"; Type primaryKeyType = ((PropertyInfo)primaryKeyProperty.MemberInfo).PropertyType; object primaryKeyRawValue = primaryKeyCommand.ExecuteScalar(); object primaryKeyValue = ConversionHelper.Convert(primaryKeyRawValue, primaryKeyType); primaryKeyProperty.Set(item, primaryKeyValue); } } transaction.Commit(); } } } }