Beispiel #1
0
        public int LaunchPackage(string sourceType, string sourceLocation, string packageName)
        {
            string packagePath;
            Package myPackage;
            Application integrationServices = new Application();

            // Combine path and file name.
            packagePath = Path.Combine(sourceLocation, packageName);

            switch (sourceType)
            {
                case "file":
                    // Package is stored as a file.
                    // Add extension if not present.
                    if (String.IsNullOrEmpty(Path.GetExtension(packagePath)))
                    {
                        packagePath = String.Concat(packagePath, ".dtsx");
                    }
                    if (File.Exists(packagePath))
                    {
                        myPackage = integrationServices.LoadPackage(packagePath, null);
                    }
                    else
                    {
                        throw new ApplicationException("Invalid file location: " + packagePath);
                    }
                    break;
                case "sql":
                    // Package is stored in MSDB.
                    // Combine logical path and package name.
                    if (integrationServices.ExistsOnSqlServer(packagePath, @".\sql2008dev", String.Empty, String.Empty))
                    {

                        myPackage = integrationServices.LoadFromSqlServer(packagePath, @".\sql2008dev", String.Empty, String.Empty, null);

                        myPackage.Variables["BatchId"].Value = @"{7DAD4503-0880-4AB3-B803-64FB6BBC25A3}";
                    }
                    else
                    {
                        throw new ApplicationException("Invalid package name or location: " + packagePath);
                    }
                    break;
                case "dts":
                    // Package is managed by SSIS Package Store.
                    // Default logical paths are File System and MSDB.
                    if (integrationServices.ExistsOnDtsServer(packagePath, "."))
                    {
                        myPackage = integrationServices.LoadFromDtsServer(packagePath, "localhost", null);
                    }
                    else
                    {
                        throw new ApplicationException("Invalid package name or location: " + packagePath);
                    }
                    break;
                default:
                    throw new ApplicationException("Invalid sourceType argument: valid values are 'file', 'sql', and 'dts'.");
            }

            var arVariables = Array.CreateInstance(typeof(Variable), myPackage.Variables.Count);
            myPackage.Variables.CopyTo(arVariables, 0);

            var execResult = (Int32)myPackage.Execute();
            return execResult;
        }