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); }
/// <summary> /// Writes the data to a csv file. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="destinationPath">The destination path.</param> /// <param name="data">The data.</param> /// <param name="columnInfoList">The column information list.</param> /// <param name="writeHeaders">if set to <c>true</c> [write headers].</param> /// <exception cref="System.ArgumentNullException"> /// data /// or /// columnInfoList /// </exception> public virtual void WriteData <T>(string destinationPath, IDataReader data, CsvColumnInfoList <T> columnInfoList, bool writeHeaders = true) { if (data == null) { throw new ArgumentNullException("data"); } var dataList = FileIOHelpers.GetEnumerableFromReader <T>(data); this.WriteData <T>(destinationPath, dataList, columnInfoList, writeHeaders); }
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> /// Gets the row value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="row">The row.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="parseErrors">The parse errors.</param> /// <param name="parseErrorMessage">The parse error message.</param> /// <param name="isNullable">if set to <c>true</c> [is nullable].</param> /// <returns>System.Nullable<T>.</returns> /// <exception cref="System.ArgumentNullException"> /// row /// or /// propertyName /// or /// parseErrors /// </exception> public virtual T?GetRowValue <T>(dynamic row, string propertyName, ref List <string> parseErrors, string parseErrorMessage = null, bool isNullable = false) where T : struct { if (row == null) { throw new ArgumentNullException("row"); } if (String.IsNullOrWhiteSpace(propertyName)) { throw new ArgumentNullException("propertyName"); } if (parseErrors == null) { throw new ArgumentNullException("parseErrors"); } string input = null; bool valueFound = false; if (FileIOHelpers.IsDynamicType(row)) { IDictionary <string, object> dict = row as IDictionary <string, object>; if (dict.ContainsKey(propertyName)) { input = Convert.ToString(row[propertyName]); valueFound = true; } } else { //the object is not a dynamic object, so get the value using reflection var prop = row.GetType().GetProperty(propertyName); if (prop != null) { input = Convert.ToString(prop.GetValue(row, null)); valueFound = true; } } if (valueFound) { return(GetRowValue <T>(input, ref parseErrors, parseErrorMessage, isNullable, propertyName)); } else { return(default(T?)); } }
/// <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); } }