Exemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="connectionString"></param>
 public MonetDbDriver(string connectionString)
 {
     Database = new MonetDatabase(connectionString.RegisterDriverConnectionParameters());
     BulkCopy = new MonetBulkCopy(Database);
 }
Exemplo n.º 2
0
        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (cbxSQLSchema.SelectedIndex == -1)
            {
                return;
            }

            var db   = new MonetDatabase(_connectionInfo.ToString());
            var cols = lsvSQLCols.CheckedItems.Count > 0
                ? string.Join(",",
                              lsvSQLCols.CheckedItems.Cast <ListViewItem>()
                              .Select(
                                  s => string.Format("{1}{0}{2}", s.Text, _dataConnector.OpenScope, _dataConnector.CloseScope)))
                : "*";
            var query = string.IsNullOrWhiteSpace(_tableInfo.Schema)
                ? string.Format("SELECT {0} FROM {2}{1}{3} ", cols, _tableInfo.Table, _dataConnector.OpenScope,
                                _dataConnector.CloseScope)
                : string.Format("SELECT {0} FROM {3}{1}{4}.{3}{2}{4} ", cols, _tableInfo.Schema, _tableInfo.Table,
                                _dataConnector.OpenScope, _dataConnector.CloseScope);
            var reader    = _dataConnector.ExecuteReader(query);
            var batchSize = Convert.ToInt32(nudBatchSize.Value);

            var prog      = TaskbarManager.Instance;
            var stopWatch = new Stopwatch();

            stopWatch.Start();

            _worker.DoWork += delegate(object o, DoWorkEventArgs args)
            {
                prog.SetProgressState(TaskbarProgressBarState.Normal);

                var bulkCopy = new MonetBulkCopy(db)
                {
                    BatchSize   = batchSize,
                    NotifyAfter = batchSize
                };

                bulkCopy.RowsCopied += delegate(object o1, RowsCopiedEventArgs args1)
                {
                    if (args1.Abort)
                    {
                        return;
                    }

                    if (_worker.CancellationPending)
                    {
                        args.Cancel = true;

                        throw new OperationCanceledException();
                    }
                    var percent = Math.Round(100 * (decimal)args1.RowsCopied / _tableInfo.RowsCount, 3,
                                             MidpointRounding.AwayFromZero);
                    var userState = new KeyValuePair <decimal, long>(percent, args1.RowsCopied);

                    _worker.ReportProgress(0, userState);
                };

                bulkCopy.Upload(_tableInfo.Schema, _tableInfo.Table, reader);
            };
            _worker.ProgressChanged += delegate(object o, ProgressChangedEventArgs args)
            {
                var userState = (KeyValuePair <decimal, long>)args.UserState;

                lblCopyElapsed.Text = $"{stopWatch.Elapsed:g}";
                pbrCopyStatus.Value = (int)userState.Key;


                prog.SetProgressValue((int)userState.Key, pbrCopyStatus.Maximum);

                Thread.Sleep(100);
            };
            _worker.RunWorkerCompleted += delegate(object o, RunWorkerCompletedEventArgs args)
            {
                if (args.Cancelled)
                {
                    MessageBox.Show(@"Bulk operation canceled!", @"Canceled", MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);

                    LoggerHelper.Write(LoggerOption.Warning, @"Bulk operation canceled!");
                }
                else if (args.Error != null)
                {
                    MessageBox.Show(args.Error.Message, @"Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    LoggerHelper.Write(LoggerOption.Error, "Error: {0}", args.Error.Message);
                }
                else
                {
                    MessageBox.Show(@"Bulk operation completed", @"Completed", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);

                    LoggerHelper.Write(LoggerOption.Info, @"Bulk operation completed");
                }

                stopWatch.Stop();
                prog.SetProgressState(TaskbarProgressBarState.NoProgress);

                pbrCopyStatus.Value = 0;
            };

            _worker.RunWorkerAsync();
        }