public void CustomEmployeesMapSqlRecord(Employee mapObject, Microsoft.SqlServer.Server.SqlDataRecord record, int rowIndex, IEnumerable <string> errors) { record.SetValue(record.GetOrdinal("SSN"), mapObject.L2SSN); record.SetValue(record.GetOrdinal("Name"), mapObject.L1Name); string errXml = errors.Count() > 0 ? FileIOHelpers.ErrorsToXml(errors, rowIndex) : null; record.SetValue(record.GetOrdinal("Errors"), errXml); }
public void MapSqlRecord(Microsoft.SqlServer.Server.SqlDataRecord record, int rowIndex, IEnumerable <string> errors) { record.SetString("SSN", this.L2SSN); record.SetString("Name", this.L1Name); string errs = errors.Count() > 0 ? FileIOHelpers.ErrorsToXml(errors, rowIndex) : null; record.SetString("Errors", errs); }
public static void MapValues(Company mapObject, SqlDataRecord record, int rowIndex, IEnumerable <string> errors) { record.SetInt32("CompanyId", mapObject.CompanyId); SetDateTime(record, "StartDate", mapObject.StartDate); SetDateTime(record, "EndDate", mapObject.EndDate); record.SetString("LegalName", mapObject.LegalName); record.SetString("DBAName", mapObject.DBAName); SetDateTime(record, "ChangeDate", mapObject.ChangeDate); record.SetString("UserId", mapObject.UserId); if (errors.Count() > 0) { record.SetString("ImportErrors", FileIOHelpers.ErrorsToXml(errors, rowIndex)); } }
/// <summary> /// Provides a custom mapper for the sql record if needed. Typically this might be the case if the columns in the destination table do not match the import type properties. /// </summary> /// <param name="record">The record.</param> /// <param name="rowIndex">Index of the row.</param> /// <param name="errors">The errors.</param> public void MapSqlRecord(SqlDataRecord record, int rowIndex, IEnumerable <string> errors) { record.SetInt32("CompanyId", this.CompanyId); Common.SetDateTime(record, "StartDate", this.StartDate); Common.SetDateTime(record, "EndDate", this.EndDate); record.SetString("LegalName", this.LegalName); record.SetString("DBAName", this.DBAName); Common.SetDateTime(record, "ChangeDate", this.ChangeDate); record.SetString("UserId", this.UserId); if (errors.Count() > 0) { record.SetString("ImportErrors", FileIOHelpers.ErrorsToXml(errors, rowIndex)); } }
/// <summary> /// Returns an enumerator that iterates through the collection. /// </summary> /// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns> public IEnumerator <SqlDataRecord> GetEnumerator() { if (_data == null || !_data.Any()) { yield break; } PropertyInfo[] properties = _importType.GetProperties(); StringComparer comparer = StringComparer.InvariantCultureIgnoreCase; this._validator = this._validator ?? new ObjectValidator(); bool?isDynamicType = null; int errorColumnOrdinal = -1; var sqlMetaArray = _sqlMetadata.ToArray(); if (_sqlMetadata.Any(x => comparer.Equals(x.Name, _errorColumn))) { SqlDataRecord tempRecord = new SqlDataRecord(sqlMetaArray); errorColumnOrdinal = tempRecord.GetOrdinal(_errorColumn); //will cause an exception if it does not exist, hence the any check tempRecord = null; } foreach (dynamic row in _data) { _rowIndex++; SqlDataRecord record = new SqlDataRecord(sqlMetaArray); List <string> errors = new List <string>(); //check the first object to see if it is a dynamic type as we dont need to run it throught the object mapper in that case if (!isDynamicType.HasValue) { isDynamicType = FileIOHelpers.IsDynamicType(row); } T rowObj = default(T); if (isDynamicType.Value) { try { rowObj = FileIOUtilities.MapObject <T>(row, _rowIndex, _validator, _fileValuesMapper, ref errors); } catch (Exception ex) { errors.Add(ex.ToString()); } } else { rowObj = row; } try { //built in data annotation validation this._validator.TryValidate(rowObj, ref errors); //custom validation if (_customValidator != null) { _customValidator.Invoke(rowObj, ref errors); } } catch (Exception ex) { errors.Add(ex.ToString()); } ISqlRecordMapper mapperObj = null; //if they provide a custom mapper use that one over the interface. if (this._customSqlMapper != null) { this._customSqlMapper.Invoke(rowObj, record, _rowIndex, errors); } else if ((mapperObj = rowObj as ISqlRecordMapper) != null) { mapperObj.MapSqlRecord(record, _rowIndex, errors); } else //last ditch effort, hopefully they don't rely on this { object val; //try to set the rows from the metadata, and the properties foreach (SqlMetaData metaData in _sqlMetadata) { string name = metaData.Name; val = null; if (!comparer.Equals(name, _errorColumn)) { var prop = properties.FirstOrDefault(x => comparer.Equals(x.Name, name)); if (prop != null && (val = prop.GetValue(rowObj, null)) != null) { record.SetValue(record.GetOrdinal(name), val); } } } //if an error column is defined, set the import errors if (errorColumnOrdinal != -1 && errors.Count != 0) { string errorMessage = FileIOHelpers.ErrorsToXml(errors, _rowIndex); record.SetString(errorColumnOrdinal, errorMessage); } } yield return(record); } }