public static PackageConfigurationSetting[] GetPackageConfigurationSettings(Microsoft.SqlServer.Dts.Runtime.Configuration c, Package p, string sVisualStudioRelativePath, bool bOfflineMode) { List <PackageConfigurationSetting> list = new List <PackageConfigurationSetting>(); if (c.ConfigurationType == DTSConfigurationType.ConfigFile || c.ConfigurationType == DTSConfigurationType.IConfigFile) { string sConfigurationString = c.ConfigurationString; if (c.ConfigurationType == DTSConfigurationType.IConfigFile) { sConfigurationString = System.Environment.GetEnvironmentVariable(c.ConfigurationString); } XmlDocument dom = new XmlDocument(); if (sConfigurationString != null) { if (sConfigurationString.Contains("\\")) { dom.Load(sConfigurationString); } else { //if it's a relative file path, then try this directory first, and if it's not there, then try the path relative to the dtsx package if (System.IO.File.Exists(sVisualStudioRelativePath + sConfigurationString)) { dom.Load(sVisualStudioRelativePath + sConfigurationString); } else { dom.Load(sConfigurationString); } } foreach (XmlNode node in dom.GetElementsByTagName("Configuration")) { list.Add(new PackageConfigurationSetting(node.Attributes["Path"].Value, node.SelectSingleNode("ConfiguredValue").InnerText)); } } } else if (c.ConfigurationType == DTSConfigurationType.SqlServer && !bOfflineMode) //not when in offline mode { string sConnectionManagerName; string sTableName; string sFilter; Microsoft.DataTransformationServices.Design.DesignUtils.ParseSqlServerConfigurationString(c.ConfigurationString, out sConnectionManagerName, out sTableName, out sFilter); ConnectionManager cm = p.Connections[sConnectionManagerName]; if (cm.OfflineMode) { return(list.ToArray()); } ISessionProperties o = cm.AcquireConnection(null) as ISessionProperties; try { IDBCreateCommand command = (IDBCreateCommand)o; ICommandText ppCommand; Guid IID_ICommandText = new Guid(0xc733a27, 0x2a1c, 0x11ce, 0xad, 0xe5, 0, 170, 0, 0x44, 0x77, 0x3d); command.CreateCommand(IntPtr.Zero, ref IID_ICommandText, out ppCommand); Guid DBGUID_DEFAULT = new Guid(0xc8b521fb, 0x5cf3, 0x11ce, 0xad, 0xe5, 0, 170, 0, 0x44, 0x77, 0x3d); ppCommand.SetCommandText(ref DBGUID_DEFAULT, "select ConfiguredValue, PackagePath from " + sTableName + " Where ConfigurationFilter = '" + sFilter.Replace("'", "''") + "'"); IntPtr PtrZero = new IntPtr(0); Guid IID_IRowset = new Guid(0xc733a7c, 0x2a1c, 0x11ce, 0xad, 0xe5, 0, 170, 0, 0x44, 0x77, 0x3d); tagDBPARAMS dbParams = null; int recordsAffected = 0; object executeResult = null; int result = ppCommand.Execute(PtrZero, ref IID_IRowset, dbParams, out recordsAffected, out executeResult); System.Reflection.BindingFlags getmethodflags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance; System.Reflection.BindingFlags getstaticflags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Static; Type chapterHandleType = ExpressionHighlighterPlugin.GetPrivateType(typeof(System.Data.OleDb.OleDbCommand), "System.Data.OleDb.ChapterHandle"); object chapterHandle = chapterHandleType.InvokeMember("DB_NULL_HCHAPTER", getstaticflags, null, null, null); System.Data.OleDb.OleDbDataReader dataReader = null; dataReader = (System.Data.OleDb.OleDbDataReader) typeof(System.Data.OleDb.OleDbDataReader).GetConstructors(getmethodflags)[0].Invoke(new object[] { null, null, 0, System.Data.CommandBehavior.SingleResult }); IntPtr intPtrRecordsAffected = new IntPtr(-1); dataReader.GetType().InvokeMember("InitializeIRowset", getmethodflags, null, dataReader, new object[] { executeResult, chapterHandle, intPtrRecordsAffected }); dataReader.GetType().InvokeMember("BuildMetaInfo", getmethodflags, null, dataReader, new object[] { }); dataReader.GetType().InvokeMember("HasRowsRead", getmethodflags, null, dataReader, new object[] { }); executeResult = null; while (dataReader.Read()) { list.Add(new PackageConfigurationSetting(dataReader["PackagePath"].ToString(), dataReader["ConfiguredValue"].ToString())); } dataReader.Close(); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(o); } } else if (c.ConfigurationType == DTSConfigurationType.EnvVariable) { list.Add(new PackageConfigurationSetting(c.PackagePath, System.Environment.GetEnvironmentVariable(c.ConfigurationString))); } else if (c.ConfigurationType == DTSConfigurationType.ParentVariable || c.ConfigurationType == DTSConfigurationType.IParentVariable) { list.Add(new PackageConfigurationSetting(c.PackagePath, "")); //can't know value at design time } else if (c.ConfigurationType == DTSConfigurationType.RegEntry) { list.Add(new PackageConfigurationSetting(c.PackagePath, (string)Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\" + c.ConfigurationString, "Value", ""))); } else if (c.ConfigurationType == DTSConfigurationType.IRegEntry) { list.Add(new PackageConfigurationSetting(c.PackagePath, (string)Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\" + System.Environment.GetEnvironmentVariable(c.ConfigurationString), "Value", ""))); } return(list.ToArray()); }
private void CopyVariables(List <Variable> variables, bool move, DtsContainer targetContainer, Package package, System.Collections.ArrayList sourceVariableDesigners) { foreach (Variable sourceVar in variables) { if (targetContainer is IDTSPackagePath && sourceVar.GetPackagePath().StartsWith(((IDTSPackagePath)targetContainer).GetPackagePath() + ".Variables[")) { throw new VariableCopyException("You are attempting to copy the variable '" + sourceVar.QualifiedName + "' to the same scope it is already in.", null); } else if (sourceVar.SystemVariable) { throw new VariableCopyException(sourceVar.QualifiedName + " is a system variable and cannot be copied or moved.", null); } } foreach (Variable sourceVar in variables) { //Variable targetVar = targetContainer.Variables.Add(sourceVar.Name, sourceVar.ReadOnly, sourceVar.Namespace, sourceVar.Value); //this is the standard way to add a variable, but it doesn't interact well with the variables tool window Variable targetVar = (Variable)packageDesigner.GetType().InvokeMember("CreateVariable", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance, null, packageDesigner, new object[] { targetContainer, sourceVar.Name, sourceVar.ReadOnly, sourceVar.Namespace, sourceVar.Value }); try { targetVar.Name = sourceVar.Name; } catch (Exception ex) { serviceProvider.DestroyComponent(targetVar); throw new VariableCopyException("Could not copy " + sourceVar.QualifiedName + " to scope \"" + targetContainer.Name + "\" because another variable with that name already exists.", ex); } //targetVar.DataType is read only targetVar.Description = sourceVar.Description; targetVar.EvaluateAsExpression = sourceVar.EvaluateAsExpression; targetVar.Expression = sourceVar.Expression; targetVar.RaiseChangedEvent = sourceVar.RaiseChangedEvent; if (move) { DtsContainer sourceContainer = FindObjectForVariablePackagePath(package, sourceVar.GetPackagePath()); changesvc.OnComponentChanging(sourceContainer, null); changesvc.OnComponentChanged(sourceContainer, null, null, null); //marks the package designer as dirty } } if (move) { #if DENALI || SQL2014 //terrible workaround to get the exact right parameter type for the DeleteVariables method in Denali. Guess calling InvokeMember against a function with a parameter of a generic type is tricky System.Collections.IList listParam = ((System.Collections.IList)System.Type.GetType("System.Collections.Generic.List`1[[" + ExpressionHighlighterPlugin.GetPrivateType(variablesToolWindowControl.GetType(), "Microsoft.DataTransformationServices.Design.VariableDesigner").AssemblyQualifiedName + "]]").GetConstructor(new Type[] { }).Invoke(new object[] { })); foreach (object o in sourceVariableDesigners) { listParam.Add(o); } variablesToolWindowControl.GetType().GetMethod("DeleteVariables", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance).Invoke(variablesToolWindowControl, new object[] { listParam }); #else variablesToolWindowControl.GetType().InvokeMember("DeleteVariables", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance, null, variablesToolWindowControl, new object[] { sourceVariableDesigners }); #endif } changesvc.OnComponentChanging(targetContainer, null); changesvc.OnComponentChanged(targetContainer, null, null, null); //marks the package designer as dirty SSISHelpers.MarkPackageDirty(package); ValidatePackage(package); }