コード例 #1
0
ファイル: MainWindowVM.cs プロジェクト: KohrAhr/SqlOverExcel
        private void RunSqlQueryProc(object o)
        {
            try
            {
                using (new WaitCursor())
                {
                    Task runQuery = new Task(() =>
                    {
                        using (ExcelCore excelIn = new ExcelCore(Model.BaseModel.ExcelFileName, AppDataCore.Settings.AceVersion, Model.BaseModel.HDR))
                        {
                            DataTable queryResult = new DataTable();

                            // Run query
                            excelIn.RunSql(Model.BaseModel.SqlQuery, ref queryResult);

                            // Populate result
                            Model.QueryResult = queryResult;
                        }
                    });

                    runQuery.Start();
                    runQuery.Wait();
                }

                WindowsUI.RunWindowDialog(() =>
                {
                    MessageBox.Show(
                        StringsFunctions.ResourceString("resQueryCompleted"),
                        StringsFunctions.ResourceString("resInfo"),
                        MessageBoxButton.OK, MessageBoxImage.Information
                        );
                }
                                          );
            }
            catch (Exception ex)
            {
                WindowsUI.RunWindowDialog(() =>
                {
                    MessageBox.Show(
                        StringsFunctions.ResourceString("resErrorDuringExecution") +
                        Environment.NewLine + Environment.NewLine + ex.InnerException.Message,
                        StringsFunctions.ResourceString("resError"),
                        MessageBoxButton.OK, MessageBoxImage.Hand
                        );
                }
                                          );
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            CoreFunctions coreFunctions = new CoreFunctions();

            // Parse command line params
            AppData appData = coreFunctions.ParseCommandLineParams(args);

            appData = coreFunctions.ValidateCommandLineParams(appData);

            if (appData.showHelp)
            {
                coreFunctions.ShowHelp();
            }
            else
            {
                // Open file and ...
                try
                {
                    using (ExcelCore excelIn = new ExcelCore(appData.inFile, appData.acever, appData.useHdr == "Y" ? true : false))
                    {
                        if (appData.showInfo)
                        {
                            coreFunctions.DisplayWorksheetInfo(excelIn);
                        }
                        else
                        {
                            DataTable queryResult = new DataTable();
                            CancellationTokenSource tokenSource = new CancellationTokenSource();

                            try
                            {
                                Console.WriteLine("\nSQL Query execution running at: {0}", DateTime.Now.ToString());

                                // Start progress...
                                coreFunctions.ShowProgressInConsole(tokenSource.Token);

                                // Start query
                                excelIn.RunSql(appData.query, ref queryResult);

                                // Cancel progress
                                coreFunctions.StopProgress(tokenSource);
                                tokenSource = null;

                                Console.WriteLine("\nSQL Query execution completed at: {0}", DateTime.Now.ToString());

                                if (appData.resultToFile)
                                {
                                    excelIn.SaveResultToExcelFile(appData.outFile, queryResult);
                                }
                                else
                                {
                                    coreFunctions.DisplayResult(queryResult);
                                }
                            }
                            catch (Exception ex)
                            {
                                // Cancel progress
                                if (tokenSource != null)
                                {
                                    coreFunctions.StopProgress(tokenSource);
                                }

                                Console.WriteLine(
                                    "\nThe an error has been occured during executing the SQL query: \nSQL Query: \"{0}\"\nFile: \"{1}\"\nError message: {2}",
                                    appData.query,
                                    appData.inFile,
                                    ex.Message
                                    );
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nThe an error has been occured during accessing Excel file \"{0}\"\nError message: {1}", appData.inFile, ex.Message);
                }
            }

            coreFunctions.ByeMessage();
        }