コード例 #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));
                }
            }
        }