Example #1
0
 private static void runInsertDPtoServer(string location)
 {
     try
     {
         string                pkgLocation;
         runtime.Package       pkg;
         runtime.Application   app;
         runtime.DTSExecResult pkgResults;
         MyEventListener       eventListener = new MyEventListener();
         pkgLocation = location;
         app         = new runtime.Application();
         pkg         = app.LoadPackage(pkgLocation, eventListener);
         pkgResults  = pkg.Execute(null, null, eventListener, null, null);
         if (pkgResults.ToString() != "Success")
         {
             Console.Write("Package run failed");
         }
     }
     catch (Exception ex)
     {
         Console.Write(ex.Message);
     }
 }
Example #2
0
        // Define the event handlers.
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            string path = PATH_LOG_SERVICE;

            using (StreamWriter writer = new StreamWriter(path, true))
            {
                writer.WriteLine(DateTime.Now + " File: " + e.FullPath + " " + e.ChangeType);
                if (File.Exists(e.FullPath))
                {
                    try
                    {
                        var fileName = System.IO.Path.GetFileNameWithoutExtension(e.FullPath);
                        if (fileName.Contains("Transaction"))
                        {
                            string[] spilit        = fileName.Split('_');
                            var      fileExtension = System.IO.Path.GetExtension(e.FullPath);
                            var      newName       = new StringBuilder();
                            if (spilit.Length < 2)
                            {
                                newName.Append(@"D:\Backup\Transaction\" + fileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension);
                                MaHoaFileExcel(e.FullPath, "TRANSACTION_DETAIL", "AccountNumber");
                                System.IO.File.Move(e.FullPath, newName.ToString());
                                throw new System.InvalidOperationException("Format invalid");
                            }
                            DateTime ReportDate;
                            if (!DateTime.TryParse(spilit[1], out ReportDate))
                            {
                                newName.Append(@"D:\Backup\Transaction\" + fileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension);
                                MaHoaFileExcel(e.FullPath, "TRANSACTION_DETAIL", "AccountNumber");
                                System.IO.File.Move(e.FullPath, newName.ToString());
                                throw new System.InvalidOperationException("Cannot parse Report Date");
                            }
                            MaHoaFileExcel(e.FullPath, "TRANSACTION_DETAIL", "AccountNumber");
                            string          pkgLocation;
                            Package         pkg;
                            Application     app;
                            DTSExecResult   pkgResults;
                            MyEventListener eventListener = new MyEventListener();
                            pkgLocation =
                                @"D:\SSIS\SSIS_Bank\Integration Services Project1\DB_SourceExcelToTrans.dtsx";
                            app = new Application();
                            pkg = app.LoadPackage(pkgLocation, eventListener);
                            pkg.Variables["User::FileFullPath"].Value = e.FullPath.ToString();
                            pkg.Variables["User::FileName"].Value     = System.IO.Path.GetFileName(e.FullPath);
                            pkg.Variables["User::ReportDate"].Value   = ReportDate;
                            pkgResults = pkg.Execute(null, null, eventListener, null, null);
//                            MaHoaFileExcel(e.FullPath);
                            var serverBackupFolder = string.Empty;
                            if (pkgResults.ToString() == "Success")
                            {
                                serverBackupFolder = @"D:\Backup\Transaction";
                                writer.WriteLine(DateTime.Now + ": Import " + System.IO.Path.GetFileName(e.FullPath) + " successfully");
                            }
                            else
                            {
                                serverBackupFolder = @"D:\Backup\Failure";
                                writer.WriteLine(DateTime.Now + ": Import " + System.IO.Path.GetFileName(e.FullPath) + " failed");
                            }


                            //writer.WriteLine(DateTime.Now + ":cb ma hoa ");

                            //writer.WriteLine(DateTime.Now + ": ma hoa thanh cong ");

                            newName.Append(serverBackupFolder + @"\" + fileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension);

                            System.IO.File.Move(e.FullPath, newName.ToString());
                        }
                        else if (fileName.Contains("Retrival"))
                        {
                            string[] spilit        = fileName.Split('_');
                            var      fileExtension = System.IO.Path.GetExtension(e.FullPath);
                            var      newName       = new StringBuilder();
                            if (spilit.Length < 2)
                            {
                                newName.Append(@"D:\Backup\Transaction\" + fileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension);
                                MaHoaFileExcel(e.FullPath, "TRANSACTION_DETAIL", "AccountNumber");
                                System.IO.File.Move(e.FullPath, newName.ToString());
                                throw new System.InvalidOperationException("Format invalid");
                            }
                            DateTime ReportDate;
                            if (!DateTime.TryParse(spilit[1], out ReportDate))
                            {
                                newName.Append(@"D:\Backup\Transaction\" + fileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension);
                                MaHoaFileExcel(e.FullPath, "TRANSACTION_DETAIL", "AccountNumber");
                                System.IO.File.Move(e.FullPath, newName.ToString());
                                throw new System.InvalidOperationException("Cannot parse Report Date");
                            }
                            MaHoaFileExcel(e.FullPath, "TRANSACTION_DETAIL", "AccountNumber");

                            string          pkgLocation;
                            Package         pkg;
                            Application     app;
                            DTSExecResult   pkgResults;
                            MyEventListener eventListener = new MyEventListener();
                            pkgLocation =
                                @"D:\SSIS\SSIS_Bank\Integration Services Project1\DB_SourceExcelToRetrival.dtsx";
                            app = new Application();
                            pkg = app.LoadPackage(pkgLocation, eventListener);
                            pkg.Variables["User::FileFullPath"].Value = e.FullPath.ToString();
                            pkg.Variables["User::FileName"].Value     = System.IO.Path.GetFileName(e.FullPath);
                            pkg.Variables["User::ReportDate"].Value   = ReportDate;
                            pkgResults = pkg.Execute(null, null, eventListener, null, null);


                            var serverBackupFolder = string.Empty;
                            if (pkgResults.ToString() == "Success")
                            {
                                serverBackupFolder = @"D:\Backup\Transaction";
                                writer.WriteLine(DateTime.Now + ": Import " + System.IO.Path.GetFileName(e.FullPath) + " successfully");
                            }
                            else
                            {
                                serverBackupFolder = @"D:\Backup\Failure";
                                writer.WriteLine(DateTime.Now + ": Import " + System.IO.Path.GetFileName(e.FullPath) + " failed");
                            }


                            //writer.WriteLine(DateTime.Now + ":cb ma hoa ");

                            //writer.WriteLine(DateTime.Now + ": ma hoa thanh cong ");
                            newName.Append(serverBackupFolder + @"\" + fileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension);
                            System.IO.File.Move(e.FullPath, newName.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        writer.WriteLine(DateTime.Now + ": Import " + System.IO.Path.GetFileName(e.FullPath) + " failed | Ex:" + ex.Message);
                    }
                }
                writer.Close();
            }
        }
Example #3
0
        public static void RunAllPackage()
        {
            string path    = @"D:\\RunningLog\\RunPackage_Log_" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".txt";
            string content = string.Empty;

            if (!Directory.Exists(@"D:\\RunningLog"))
            {
                Directory.CreateDirectory(@"D:\\RunningLog");
            }
            DateTime now = DateTime.Now;

            try
            {
                string                pkgLocation;
                runtime.Package       pkg;
                runtime.Application   app;
                runtime.DTSExecResult pkgResults;
                MyEventListener       eventListener = new MyEventListener();
                pkgLocation = @"D:\SSIS\SSIS_Bank\Integration Services Project1\DB_TransToDaily.dtsx";;
                app         = new runtime.Application();
                pkg         = app.LoadPackage(pkgLocation, eventListener);
                pkgResults  = pkg.Execute(null, null, eventListener, null, null);
                if (pkgResults.ToString() == "Success")
                {
                    content += DateTime.Now.ToString() + ": Execute Build Daily successfully\n";
                }
                else
                {
                    content += DateTime.Now.ToString() + ": Execute Build Daily failed\n";
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                content += DateTime.Now.ToString() + ": Error -> " + ex.Message.ToString() + "\n";
                System.IO.File.WriteAllText(path, content);
            }
            if (now.Day == 1)
            {
                try
                {
                    string                pkgLocation;
                    runtime.Package       pkg;
                    runtime.Application   app;
                    runtime.DTSExecResult pkgResults;
                    MyEventListener       eventListener = new MyEventListener();
                    pkgLocation = @"D:\SSIS\SSIS_Bank\Integration Services Project1\DB_DailyToMonthly.dtsx";
                    app         = new runtime.Application();
                    pkg         = app.LoadPackage(pkgLocation, eventListener);
                    DateTime yesterday = DateTime.Now.AddDays(-1);
                    pkg.Variables["User::nam"].Value   = yesterday.Year;
                    pkg.Variables["User::thang"].Value = yesterday.Month;
                    pkgResults = pkg.Execute(null, null, eventListener, null, null);
                    if (pkgResults.ToString() == "Success")
                    {
                        content += DateTime.Now.ToString() + ": Execute Build Monthly successfully\n";
                    }
                    else
                    {
                        content += DateTime.Now.ToString() + ": Execute Build Monthly failed\n";
                    }
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    content += DateTime.Now.ToString() + ": Error -> " + ex.Message.ToString() + "\n";
                    System.IO.File.WriteAllText(path, content);
                }
                if (now.Month == 1 || now.Month == 4 || now.Month == 7 || now.Month == 10)
                {
                    try
                    {
                        string                pkgLocation;
                        runtime.Package       pkg;
                        runtime.Application   app;
                        runtime.DTSExecResult pkgResults;
                        MyEventListener       eventListener = new MyEventListener();
                        pkgLocation = @"D:\SSIS\SSIS_Bank\Integration Services Project1\DB_MonthlyToQuarterly.dtsx";
                        app         = new runtime.Application();
                        pkg         = app.LoadPackage(pkgLocation, eventListener);
                        DateTime yesterday = DateTime.Now.AddDays(-1);
                        int      du        = yesterday.Month % 3 == 0 ? 0 : 1;
                        int      quy       = yesterday.Month / 3 + du;

                        pkg.Variables["User::nam"].Value = yesterday.Year;
                        pkg.Variables["User::quy"].Value = quy;
                        pkgResults = pkg.Execute(null, null, eventListener, null, null);
                        if (pkgResults.ToString() == "Success")
                        {
                            content += DateTime.Now.ToString() + ": Execute Build Quarterly successfully\n";
                        }
                        else
                        {
                            content += DateTime.Now.ToString() + ": Execute Build Quarterly failed\n";
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        content += DateTime.Now.ToString() + ": Error -> " + ex.Message.ToString() + "\n";
                        System.IO.File.WriteAllText(path, content);
                    }
                }
                if (now.Month == 1)
                {
                    try
                    {
                        string                pkgLocation;
                        runtime.Package       pkg;
                        runtime.Application   app;
                        runtime.DTSExecResult pkgResults;
                        MyEventListener       eventListener = new MyEventListener();
                        pkgLocation = @"D:\SSIS\SSIS_Bank\Integration Services Project1\DB_QuarterlyToYearly.dtsx";
                        app         = new runtime.Application();
                        pkg         = app.LoadPackage(pkgLocation, eventListener);
                        DateTime yesterday = DateTime.Now.AddDays(-1);
                        pkg.Variables["User::nam"].Value = yesterday.Year;
                        pkgResults = pkg.Execute(null, null, eventListener, null, null);
                        if (pkgResults.ToString() == "Success")
                        {
                            content += DateTime.Now.ToString() + ": Execute Build Yearly successfully\n";
                        }
                        else
                        {
                            content += DateTime.Now.ToString() + ": Execute Build Yearly failed\n";
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        content += DateTime.Now.ToString() + ": Error -> " + ex.Message.ToString() + "\n";
                        System.IO.File.WriteAllText(path, content);
                    }
                }
            }
            try
            {
                runInsertDPtoServer(@"D:\SSIS\SSIS_Bank\Integration Services Project1\SERVER_DBTransToServer.dtsx");
                runInsertDPtoServer(@"D:\SSIS\SSIS_Bank\Integration Services Project1\SERVER_RetrivalToServer.dtsx");
                runInsertDPtoServer(@"D:\SSIS\SSIS_Bank\Integration Services Project1\SERVER_Transaction_Detail_InvalidToServer.dtsx");
                runInsertDPtoServer(@"D:\SSIS\SSIS_Bank\Integration Services Project1\SERVER_Retrival_InvalidToServer.dtsx");
                runInsertDPtoServer(@"D:\SSIS\SSIS_Bank\Integration Services Project1\SERVER_DBDailyToServer.dtsx");
                runInsertDPtoServer(@"D:\SSIS\SSIS_Bank\Integration Services Project1\SERVER_DBMonthLyToServer.dtsx");
                runInsertDPtoServer(@"D:\SSIS\SSIS_Bank\Integration Services Project1\SERVER_DBQuarterLyToServer.dtsx");
                runInsertDPtoServer(@"D:\SSIS\SSIS_Bank\Integration Services Project1\SERVER_DBYearlyToServer.dtsx");
                content += DateTime.Now.ToString() + ": Insert to DB Server successfully\n";
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                content += DateTime.Now.ToString() + ": Error -> " + ex.Message.ToString() + "\n";
                System.IO.File.WriteAllText(path, content);
            }
            content += DateTime.Now.ToString() + ": Packages run successfully";
            System.IO.File.WriteAllText(path, content);
        }