/// <summary>Writes a value to a variable.</summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="source">The source.</param> /// <param name="name">The variable name.</param> /// <param name="value">The value.</param> /// <exception cref="KeyNotFoundException">The variable could not be found.</exception> /// <remarks> /// The variable is locked for writing while the write is occurring. /// </remarks> public static void SetValue <T> (this VariableDispenser source, string name, T value) { source.LockForWrite(name); Variables variables = null; try { source.GetVariables(ref variables); variables.SetVar <T>(name, value); } finally { if (variables != null) { variables.Unlock(); } }; }
/// <summary> /// Executes tasks. Does the actual sleep /// </summary> /// <param name="connections"></param> /// <param name="variableDispenser"></param> /// <param name="componentEvents"></param> /// <param name="log"></param> /// <param name="transaction"></param> /// <returns></returns> public override DTSExecResult Execute(Connections connections, VariableDispenser variableDispenser, IDTSComponentEvents componentEvents, IDTSLogging log, object transaction) { DTSExecResult chk = Validate(connections, variableDispenser, componentEvents, log); if (chk == DTSExecResult.Failure) { return(chk); } XElement variablesElement = new XElement(RootElementName); Variables vars = null; try { //Lock XmlVariable for writing variableDispenser.LockForWrite(XmlVariable); //Lock all export variables for reading foreach (string variable in _exportVariables) { variableDispenser.LockForRead(variable); } variableDispenser.GetVariables(ref vars); if (vars != null && vars.Locked == false) { componentEvents.FireError(0, Resources.VariablesToXmlTaskName, Resources.ErrorLockingVariables, string.Empty, 0); return(DTSExecResult.Failure); } else { //build the export exml foreach (string var in _exportVariables) { Variable v = vars[var]; XElement varElement = new XElement(_variableElementName); varElement.Add(new XAttribute("name", v.QualifiedName)); if (ExportDataType) { varElement.Add(new XAttribute("dataType", v.DataType)); } if (v.Value == null) { varElement.Add(new XAttribute("isNull", true)); } else { Type ot = v.Value.GetType(); if (ExportValueDataType) { varElement.Add(new XAttribute("valueDataType", ot.FullName)); } switch (v.DataType) { case TypeCode.DateTime: varElement.SetValue(((DateTime)v.Value).ToString(DateTimeFormatInfo.InvariantInfo.SortableDateTimePattern)); break; case TypeCode.DBNull: varElement.Add(new XAttribute("isDBNull", true)); break; case TypeCode.Empty: varElement.Add(new XAttribute("isEmpty", true)); break; case TypeCode.Object: object o = v.Value; if (ot == typeof(TimeSpan)) { varElement.SetValue(Convert.ToString(o, CultureInfo.InvariantCulture)); } else if (ot.IsEnum) { varElement.SetValue(Enum.GetName(ot, o)); } else if (ExportBinaryData) { //TODO: Implement other data types varElement.Add(new XAttribute("notImplementedDataType", true)); } else { varElement.Add(new XAttribute("binaryDataNotExported", true)); varElement.SetValue(ot.FullName); } break; default: string s = Convert.ToString(v.Value, CultureInfo.InvariantCulture); varElement.SetValue(s); break; } } if (ExportVariablePath) { varElement.Add(new XAttribute("path", v.GetPackagePath())); } if (ExportDescription && v.Description != string.Empty) { varElement.Add(new XAttribute("description", v.Description)); } variablesElement.Add(varElement); } Variable xmlVar = vars[XmlVariable]; xmlVar.Value = variablesElement.ToString(XmlSaveOptions); } } finally { if (vars != null && vars.Locked) { vars.Unlock(); } } return(base.Execute(connections, variableDispenser, componentEvents, log, transaction)); }