예제 #1
0
파일: SqlData.cs 프로젝트: EtherZa/sqlcopy
        //public void Copy(TableObject table)
        //{
        //	if (CopySettings.DeleteRows) this.Delete(table);

        //	using (IDataReader dr = this.Select(table))
        //	{
        //		using (SqlBulkCopy copy = new SqlBulkCopy(CopySettings.Destination, this.Options))
        //		{
        //			copy.BulkCopyTimeout = CopySettings.BulkCopyTimeout;
        //			copy.BatchSize = CopySettings.BatchSize;
        //			copy.DestinationTableName = this.FullTableName(table);
        //			copy.NotifyAfter = CopySettings.NotifyAfter;
        //			copy.SqlRowsCopied += copy_SqlRowsCopied;
        //			copy.WriteToServer(dr);
        //		}
        //	}
        //}

        //void copy_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
        //{
        //	if (OnRowsCopied != null) { OnRowsCopied(this, new RowsCopiedEventArgs(e)); }
        //}

        // Copy from source to this destination
        public void Copy(TableObject table, IDbData source)
        {
            // delete from destination (this)
            if (CopySettings.DeleteRows)
            {
                this.Delete(table);
            }

            // select from source
            using (IDataReader dr = source.Select(table))
            {
                // copy to destination (this)
                using (SqlBulkCopy copy = new SqlBulkCopy(this.ConnectionString, this.Options))
                {
                    copy.BulkCopyTimeout      = CopySettings.BulkCopyTimeout;
                    copy.BatchSize            = CopySettings.BatchSize;
                    copy.DestinationTableName = this.FullTableName(table);
                    copy.NotifyAfter          = CopySettings.NotifyAfter;
                    //copy.SqlRowsCopied += copy_SqlRowsCopied;
                    copy.SqlRowsCopied += table.OnRowsCopied;
                    copy.WriteToServer(dr);

                    // update final count
                    var rowsCopiedField = typeof(SqlBulkCopy).GetField("_rowsCopied", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
                    var rowCount        = (int)rowsCopiedField.GetValue(copy);
                    table.OnRowsCopied(copy, new SqlRowsCopiedEventArgs(rowCount));
                }
            }
        }
예제 #2
0
        //public void Copy(TableObject table)
        //{
        //	if (CopySettings.DeleteRows) this.Delete(table);

        //	using (IDataReader dr = (IDataReader)this.Select(table))
        //	{
        //		MySqlBulkCopy copy = new MySqlBulkCopy(CopySettings.Destination, this.Options);
        //		//MySqlBulkCopy copy = new MySqlBulkCopy();
        //		//copy.BatchSize = settings.BatchSize;
        //		copy.DestinationTableName = this.FullTableName(table);
        //		copy.DestinationTableName = copy.DestinationTableName.Replace("[", "");
        //		copy.DestinationTableName = copy.DestinationTableName.Replace("]", "");

        //		//// map all items
        //		//ColumnMapItemCollection collection = new ColumnMapItemCollection();
        //		//for (int i = 0; i < dr.FieldCount; i++ )
        //		//{
        //		//	dr.GetDataTypeName(i);
        //		//	ColumnMapItem item = new ColumnMapItem();
        //		//	item.DataType = dr.GetDataTypeName(i);
        //		//	item.DestinationColumn = dr.GetName(i);
        //		//	item.SourceColumn = dr.GetName(i);
        //		//	collection.Add(item);
        //		//}
        //		//copy.ColumnMapItems = collection;

        //		//copy.DestinationDbConnection = connection;

        //		//copy.OnBatchSizeCompleted +=
        //		//copy.OnBatchSizeCompleted = new IndiansInc.MySqlBulkCopy.OnBatchSizeCompletedDelegate(this.copy_SqlRowsCopied);

        //		//OnBatchSizeCompletedDelegate test = copy_SqlRowsCopied;

        //		//copy.OnBatchSizeCompleted += copy_OnBatchSizeCompleted;

        //		copy.SqlRowsCopied += table.OnRowsCopied;

        //		//copy.BulkCopyTimeout = settings.BulkCopyTimeout;

        //		copy.NotifyAfter = CopySettings.NotifyAfter;
        //		//copy.SqlRowsCopied += copy_SqlRowsCopied;
        //		//copy.WriteToServer(dr);
        //		copy.WriteToServer(dr);
        //	}
        //}

        //void copy_SqlRowsCopied(System.Data.SqlClient.SqlRowsCopiedEventArgs e)
        //{
        //	throw new NotImplementedException();
        //}

        //void copy_OnBatchSizeCompleted(BatchSizeCompletedEventArgs e)
        //{
        //	if (OnRowsCopied != null) { OnRowsCopied(this, new RowsCopiedEventArgs(e)); }
        //}

        //void copy_SqlRowsCopied(object sender, BatchSizeCompletedEventArgs e)
        //{
        //	if (OnRowsCopied != null) { OnRowsCopied(this, new RowsCopiedEventArgs(e)); }
        //}


        // Copy from source to destination (this = destination)
        public void Copy(TableObject table, IDbData source)
        {
            // delete from destination (this)
            if (CopySettings.DeleteRows)
            {
                this.Delete(table);
            }

            // seletc from source
            using (IDataReader dr = source.Select(table))
            {
                // copy to destination this
                using (MySqlBulkCopy copy = new MySqlBulkCopy(this.ConnectionString, this.Options))
                {
                    //MySqlBulkCopy copy = new MySqlBulkCopy();
                    copy.BatchSize = CopySettings.BatchSize;

                    copy.DestinationTableName = this.FullTableName(table);
                    copy.DestinationTableName = copy.DestinationTableName.Replace("[", "");
                    copy.DestinationTableName = copy.DestinationTableName.Replace("]", "");

                    // map all items
                    ColumnMapItemCollection collection = new ColumnMapItemCollection();
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        dr.GetDataTypeName(i);
                        ColumnMapItem item = new ColumnMapItem();
                        item.DataType          = dr.GetDataTypeName(i);
                        item.DestinationColumn = dr.GetName(i);
                        item.SourceColumn      = dr.GetName(i);
                        collection.Add(item);
                    }
                    copy.ColumnMappings = collection;

                    //copy.DestinationDbConnection = connection;
                    //copy.BulkCopyTimeout = settings.BulkCopyTimeout;
                    copy.NotifyAfter = CopySettings.NotifyAfter;
                    //copy.SqlRowsCopied += table.OnRowsCopied;


                    //copy.WriteToServer(dr);
                    copy.WriteToServer(dr);
                }
            }
        }
예제 #3
0
        //public void Copy(TableObject table)
        //{
        //	// Delete data
        //	if (CopySettings.DeleteRows) this.Delete(table);


        //	using (IDataReader dr = this.Select(table))
        //	{
        //		using (OracleBulkCopy copy = new OracleBulkCopy(CopySettings.Destination, this.Options))
        //		{
        //			copy.BulkCopyTimeout = CopySettings.BulkCopyTimeout;
        //			copy.BatchSize = CopySettings.BatchSize;
        //			copy.DestinationTableName = this.FullTableName(table);
        //			copy.NotifyAfter = CopySettings.NotifyAfter;
        //			copy.OracleRowsCopied += copy_OracleRowsCopied;
        //			copy.WriteToServer(dr);
        //		}
        //	}
        //}

        // Copy from source to destination this
        public void Copy(TableObject table, IDbData source)
        {
            // Delete data from destination (this)
            if (CopySettings.DeleteRows)
            {
                this.Delete(table);
            }

            // read from source
            using (IDataReader dr = source.Select(table))
            {
                // copy to destination (this)
                using (OracleBulkCopy copy = new OracleBulkCopy(this.ConnectionString, this.Options))
                {
                    copy.BulkCopyTimeout      = CopySettings.BulkCopyTimeout;
                    copy.BatchSize            = CopySettings.BatchSize;
                    copy.DestinationTableName = this.FullTableName(table);
                    copy.NotifyAfter          = CopySettings.NotifyAfter;
                    //copy.OracleRowsCopied += copy_OracleRowsCopied;
                    copy.WriteToServer(dr);
                }
            }
        }