Esempio n. 1
0
		private void StartBulkCopy()
		{
			if (_cpDest == null)
			{
				MessageService.ShowError("Destination connection not specified!");
				return;
			}

			string confirmMsg = String.Format(Properties.Resources.CopyDataConfirmation, _cp.InfoDbServer, _cpDest.InfoDbServer);
			SqlBulkCopyOptions ops = EvalSqlBulkCopyOptions();
			if ((ops & SqlBulkCopyOptions.TableLock) == SqlBulkCopyOptions.TableLock)
			{
				int majorServerVersion = SmoHelpers.GetServerMajorVersion(_cpDest);
				if (majorServerVersion == 9 && !MessageService.AskQuestion(Properties.Resources.TableLock_Notification + confirmMsg))
					return;
			}
			else if (!MessageService.AskQuestion(confirmMsg))
				return;

			BulkCopyArgs args = new BulkCopyArgs();
			args.Objects = objList.SelectedObjects;
			args.StopOnError = chkStopOnError.Checked;
			args.CopyOptions = EvalSqlBulkCopyOptions();
			_copying = true;
			_cancelled = false;

			wizardControl1.NextButtonEnabled = false;
			wizardControl1.BackButtonEnabled = false;

			_totalCopied = 0;
			lblTotal.Text = String.Empty;

			StartTimer();

			bw.RunWorkerAsync(args);
		}
Esempio n. 2
0
		private void bw_DoWork(object sender, DoWorkEventArgs e)
		{

			BulkCopyArgs args = e.Argument as BulkCopyArgs;
			if (args == null)
				return;

			BulkCopyResult result = new BulkCopyResult();


			ProgressData pd = new ProgressData();
			pd.Total = args.Objects.Count;
			SqlTransaction tr = null;
			string copyErrors = String.Empty;
			bool copyCancelled = false;
			SqlConnection sourceConn = null;
			SqlConnection destConn = null;

			try
			{
				sourceConn = _cp.CreateSqlConnection(true, false);
				destConn = _cpDest.CreateSqlConnection(true, false);
				try
				{
					if (args.StopOnError)
						tr = destConn.BeginTransaction();


					try
					{
						if (rbEmpty.Checked)
						{
							DeleteData(destConn, tr, args.Objects);
						}
						else if (rbEmptyAll.Checked)
						{
							DeleteAllData(destConn, tr);						
						}
					}
					catch (Exception ex)
					{
						if (args.StopOnError)
							tr.Rollback();

						result.Errors += SmoHelpers.FormatExceptionMsg(ex);
						return;
					}

					foreach (DbObjectList.DbObjectInfo selObj in args.Objects)
					{
						try
						{
							pd.Info = selObj.Name;
							pd.Progress++;

							if (_cancelled || bw.CancellationPending)
							{
								e.Cancel = true;
								if (args.StopOnError)
									tr.Rollback();

								break;
							}
							else
							{
								bw.ReportProgress(0, pd);
								if (!LoadDataAndCopy(selObj.SchemaAndName, args.CopyOptions, sourceConn, destConn, tr, out copyErrors, out copyCancelled))
								{
									if (args.StopOnError)
										tr.Rollback();

									if (copyCancelled)
									{
										e.Cancel = true;
										break;
									}


									result.Errors += copyErrors;
									if (args.StopOnError)
										break;
								}
							}
						}
						catch (Exception ex)
						{
							if (args.StopOnError)
							{
								tr.Rollback();
								result.Errors += SmoHelpers.FormatExceptionMsg(ex, "Table/View: " + selObj.Name);
								break;
							}
							else
							{
								result.Errors += SmoHelpers.FormatExceptionMsg(ex, "Table/View: " + selObj.Name);
							}
						}
					}

					if (args.StopOnError && tr.Connection != null)
						tr.Commit();
				}
				catch (Exception ex)
				{
					result.Errors += SmoHelpers.FormatExceptionMsg(ex);
				}
			}
			finally
			{
				e.Result = result;

				if (sourceConn != null)
				{
					if (sourceConn.State != ConnectionState.Closed)
						sourceConn.Close();
					sourceConn.Dispose();
				}

				if (destConn != null)
				{
					if (destConn.State != ConnectionState.Closed)
						destConn.Close();
					destConn.Dispose();
				}
			}
		}
Esempio n. 3
0
		private bool LoadDataAndCopy(string tableName, SqlBulkCopyOptions options, SqlConnection sourceConn, SqlConnection destConn, SqlTransaction tr, out string errors, out bool cancelled)
		{
			bool result = true;
			string[] strTable = tableName.Split('.');
			SqlCommand sourceCmd = null;
			SqlDataReader sourceReader = null;

			SqlBulkCopy bulkCopy = null;
			SqlCommand mapCmd = null;
			errors = String.Empty;
			cancelled = false;
			try
			{
				NotifyTotalRows(tableName, sourceConn);

				//Load data
				sourceCmd = new SqlCommand("SELECT * FROM " + tableName, sourceConn);
				sourceCmd.CommandTimeout = 0;
				sourceReader = sourceCmd.ExecuteReader();


				//Map Columns
				bulkCopy = new SqlBulkCopy(destConn, options, tr);
				bulkCopy.NotifyAfter = 1;
				bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);

				//mapCmd = new SqlCommand("SELECT COLUMN_NAME,COLUMNPROPERTY( OBJECT_ID('" + strTable[0] + "." + strTable[1] + "'),COLUMN_NAME,'IsComputed')AS 'IsComputed' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" + strTable[0] + "' AND TABLE_NAME = '" + strTable[1] + "'", destConn);
				mapCmd = new SqlCommand(String.Format(Properties.Resources.Script_ColumnMap, strTable[0], strTable[1], strTable[0], strTable[1]), destConn);
				mapCmd.Transaction = tr;

				SqlDataReader mapColReader = null;
				try
				{
					mapColReader = mapCmd.ExecuteReader();
					while (mapColReader.Read())
					{
						if (mapColReader.GetInt32(1) != 1)
							bulkCopy.ColumnMappings.Add(mapColReader.GetString(0), mapColReader.GetString(0));
					}

				}
				finally
				{
					if (!mapColReader.IsClosed)
						mapColReader.Close();
					mapColReader.Dispose();
				}

				//Copy data
				bulkCopy.BatchSize = 50;
				bulkCopy.BulkCopyTimeout = 0;
				bulkCopy.DestinationTableName = tableName;
				bulkCopy.WriteToServer(sourceReader);
			}
			catch (OperationAbortedException)
			{
				cancelled = true;
				result = false;
			}
			catch (Exception ex)
			{
				result = false;
				errors = SmoHelpers.FormatExceptionMsg(ex, "Table/View: " + tableName);
			}
			finally
			{
				if (sourceCmd != null)
					sourceCmd.Dispose();

				if (sourceReader != null)
				{
					if (!sourceReader.IsClosed)
						sourceReader.Close();

					sourceReader.Dispose();
				}

				if (mapCmd != null)
					mapCmd.Dispose();

				if (bulkCopy != null)
				{
					bulkCopy.Close();
				}
			}
			return result;
		}