예제 #1
0
        private void selectByCountryButton_Click(object sender, EventArgs e)
        {
            if (countriesCombox.Text == "Select")
            {
                MessageBox.Show("Please select a country");
                return;
            }

            var ops = new SqlServerOperations();

            var item = (CountryItem)countriesCombox.SelectedItem;
            var destinationFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{item.Compact}.xlsx");

            if (CreateNewExcelFileCheckBox.Checked)
            {
                if (!(ops.CopyToApplicationFolder(_companyFileName, destinationFileName)))
                {
                    MessageBox.Show("Copy failed");
                    return;
                }
            }

            var rowCount = 0;

            if (ops.ExportByCountryNameCustomersToExcel(destinationFileName, countriesCombox.Text, ref rowCount))
            {
                MessageBox.Show($"Exported {rowCount} rows.");
            }
            else
            {
                MessageBox.Show(ops.ExceptionMessage);
            }
        }
        /// <summary>
        /// Export from SQL-Server Customers table selected fields.
        /// The same can be done by writing a method for exporting to Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdExportFromSqlServerToMsAccessDynamic_Click(object sender, EventArgs e)
        {
            var columnList = clbColumnNames.CheckedIColumnDetailsList();

            if (columnList.Count > 0)
            {
                /*
                 * if chkBracketed is checked wrap field names with [], otherwise
                 * use field names without brackets.
                 */
                var fields = chkBracketed.Checked
                    ? string.Join(",", columnList.Select(col => col.NameBracketed))
                    : string.Join(",", columnList.Select(col => col.Name));

                var ops = new SqlServerOperations();

                if (!(ops.CopyToApplicationFolder(_accessCompanyFileName)))
                {
                    MessageBox.Show(ops.LastExceptionMessage);
                    return;
                }

                int rowCount = 0;

                MessageBox.Show(
                    ops.ExportAllCustomersToAccess(_accessCompanyFileName, countriesComboxAccess2.Text, fields, ref rowCount)
                        ? $"Exported {rowCount} rows."
                        : ops.LastExceptionMessage);
            }
            else
            {
                MessageBox.Show("Please select one or more fields and try again");
            }
        }
        /// <summary>
        /// Export all customer records or by country
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdExportFromSqlServerToMsAccess_Click(object sender, EventArgs e)
        {
            var ops = new SqlServerOperations();

            if (!(ops.CopyToApplicationFolder(_accessCompanyFileName)))
            {
                MessageBox.Show(ops.LastExceptionMessage);
                return;
            }

            int rowCount = 0;

            MessageBox.Show(
                ops.ExportAllCustomersToAccess(_accessCompanyFileName, countriesComboxAccess1.Text, ref rowCount)
                    ? $"Exported {rowCount} rows."
                    : ops.LastExceptionMessage);
        }
        /// <summary>
        /// Export all customers table records to Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectAllButton_Click(object sender, EventArgs e)
        {
            var ops = new SqlServerOperations();

            if (CreateNewExcelFileCheckBox.Checked)
            {
                if (!(ops.CopyToApplicationFolder(_excelCompanyFileName)))
                {
                    MessageBox.Show(ops.LastExceptionMessage);
                    return;
                }
            }

            int rowCount = 0;

            MessageBox.Show(ops.ExportAllCustomersToExcel(_excelCompanyFileName, ref rowCount)
                ? $"Exported {rowCount} rows."
                : ops.LastExceptionMessage);
        }