private static void MarshallProperty(T item, IDataRecord reader, ColumnAttribute column, DynamicMember property) { if (reader[column.Name].Equals(DBNull.Value)) { if (column.Nullable) { property.Set(item, null); } else { throw new ArgumentNullException(SR.GetString(SR.RowDataGateway_Null_Property, new object[] { property.MemberInfo.Name })); } } else { property.Set(item, reader[column.Name]); } }
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(); } } } }