Exemple #1
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);
                }
            }
        }
Exemple #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            MySql.Data.MySqlClient.MySqlConnection connection       = new MySql.Data.MySqlClient.MySqlConnection("Server=localhost;Port=3306;Database=destination;Uid=root;Pwd=12345;");
            MySql.Data.MySqlClient.MySqlConnection sourceConnection = new MySql.Data.MySqlClient.MySqlConnection("Server=localhost;Port=3306;Database=users;Uid=root;Pwd=12345;");
            try
            {
                connection.Open();
                sourceConnection.Open();
                DataTable     table  = new DataTable();
                MySqlBulkCopy upload = new MySqlBulkCopy();
                upload.DestinationTableName = "session";
                ColumnMapItemCollection collection = new ColumnMapItemCollection();
                ColumnMapItem           sessionId  = new ColumnMapItem();
                ColumnMapItem           userId     = new ColumnMapItem();
                ColumnMapItem           dateLogged = new ColumnMapItem();
                ColumnMapItem           loggedFrom = new ColumnMapItem();
                ColumnMapItem           active     = new ColumnMapItem();

                sessionId.DataType          = "text";
                sessionId.DestinationColumn = "IdSession";
                sessionId.SourceColumn      = "IdSession";

                userId.DataType          = "int";
                userId.DestinationColumn = "userid";
                userId.SourceColumn      = "userid";

                dateLogged.DataType          = "datetime";
                dateLogged.DestinationColumn = "dateLogged";
                dateLogged.SourceColumn      = "dateLogged";

                loggedFrom.DataType          = "text";
                loggedFrom.DestinationColumn = "loggedFrom";
                loggedFrom.SourceColumn      = "loggedFrom";

                active.DataType          = "int";
                active.DestinationColumn = "active";
                active.SourceColumn      = "active";

                collection.Add(sessionId);
                collection.Add(userId);
                collection.Add(dateLogged);
                collection.Add(loggedFrom);
                collection.Add(active);

                upload.ColumnMapItems          = collection;
                upload.DestinationDbConnection = connection;

                MySql.Data.MySqlClient.MySqlCommand    command = new MySql.Data.MySqlClient.MySqlCommand("select idsession,userid,datelogged,loggedfrom,active from session", sourceConnection);
                MySql.Data.MySqlClient.MySqlDataReader reader  = command.ExecuteReader();
                upload.Upload(reader);
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            connection.Close();
            connection.Dispose();
            sourceConnection.Close();
            sourceConnection.Dispose();
        }