How to Copy Row from Same Table on Diferent Databases

Posts   
 
    
Posts: 5
Joined: 09-Nov-2005
# Posted on: 09-May-2006 06:46:15   

Hello,

I want to copy all the data from database A Table Admin to Database 2 Table Admin. Both tables are identical, but each one belong to a diferent DataLayer. Both are Access Databases.

Data Layers are generated using:

Name: SelfServicing, general scenario Vs.Net 2005 Version: 1.0.2005.1.10232005 Vendor: Solutions Design Description: SelfServicing. Generates one entity class per entity. Validator classes are initially created but never overwritten. It will also create a VS.NET 2005 project file, however will never overwrite this file.

I tried the folliwng code bellow



//Create Collection Objects
 FPS_DataLayer.CollectionClasses.AdminCollection AdminTableSource = new FPS_DataLayer.CollectionClasses.AdminCollection();
Geo_DataLayer.CollectionClasses.AdminCollection AdminTableTarget = new Geo_DataLayer.CollectionClasses.AdminCollection();

            //Full table loads
            AdminTableSource.GetMulti(null);
            AdminTableTarget.GetMulti(null);
            //Delete all records
            AdminTableTarget.DeleteMulti(Geo_DataLayer.HelperClasses.AdminFields.StdId > 0);

            //Create Iteration Object
            foreach (FPS_DataLayer.EntityClasses.AdminEntity adminSource in AdminTableSource.Items)
            {
        
                Geo_DataLayer.EntityClasses.AdminEntity adminTarget = (Object)adminSource;
                


                AdminTableTarget.Add(adminTarget );
            }

            AdminTableTarget.SaveMulti();


I got an error with the Object Conversion, becuase the EntityClass from the FPS and the Geo_DataLayer are not the same...althoguth they represent the same object/row.

How do I solve this issue. Do I map field by field like: ObjectA.Field1 = Objectb.Field2??? or there is another faster way??

Thanks,

BG.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-May-2006 09:00:01   

You may try to copy/clone the fields as in the following thread: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=1834

Posts: 5
Joined: 09-Nov-2005
# Posted on: 10-May-2006 01:28:10   

Walaa,

I tried what you suggested without any luck. I had to actually do a field by field mapping to actually get it work. Not efficient at all, but it works.

I would really appreciate if you could post a code example to improve this.

Thanks,

Patrick



bool success = false;

            //TODO: Create 1-1 field mapping between Cruise Tables

            //Create Collection Objects
            FPS_DataLayer.CollectionClasses.SilvicsCollection silvTableSource = new FPS_DataLayer.CollectionClasses.SilvicsCollection();
            Geo_DataLayer.CollectionClasses.SilvicsCollection silvTableTarget = new Geo_DataLayer.CollectionClasses.SilvicsCollection();

            //Delete all records
            silvTableTarget.DeleteMulti(Geo_DataLayer.HelperClasses.SilvicsFields.Regime != "");

            //Full table loads
            silvTableSource.GetMulti(null);
            silvTableTarget.GetMulti(null);

            if (silvTableSource.Items.Count > 0)
            {
                //Create Iteration Object
                foreach (FPS_DataLayer.EntityClasses.SilvicsEntity silvSource in silvTableSource.Items)
                {
                    //Create Cruise Target Element
                    Geo_DataLayer.EntityClasses.SilvicsEntity silvTarget = new Geo_DataLayer.EntityClasses.SilvicsEntity();

                    silvTarget.Regime = silvSource.Regime;
                    silvTarget.Group = silvSource.Group;
                    silvTarget.TrtNbr = silvSource.TrtNbr;
                    silvTarget.Siteprep = silvSource.Siteprep;
                    silvTarget.TrtKey = silvSource.TrtKey;

                    silvTarget.TrtVal = silvSource.TrtVal;
                    silvTarget.ThinMeth = silvSource.ThinMeth;
                    silvTarget.ThinMv = silvSource.ThinMv;
                    silvTarget.ThinLvl = silvSource.ThinLvl;
                    silvTarget.ThinLvlv = silvSource.ThinLvlv;

                    silvTarget.ThinSp = silvSource.ThinSp;
                    silvTarget.FertRate = silvSource.FertRate;
                    silvTarget.TrtCost = silvSource.TrtCost;

                    //Add record to the collection
                    silvTableTarget.Add(silvTarget);
                }
                //Save all records into the Database
                if (silvTableTarget.SaveMulti() > 0)
                    success = true;
            }
            else 
            {
                success = true;
            }


Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-May-2006 08:16:34   
foreach (FPS_DataLayer.EntityClasses.SilvicsEntity silvSource in silvTableSource.Items)
{
  //Create Cruise Target Element
  Geo_DataLayer.EntityClasses.SilvicsEntity silvTarget = new Geo_DataLayer.EntityClasses.SilvicsEntity();

silvTarget.Fields = (IEntityFields2)((EntityFields2)silvSource.Fields).Clone();
silvTarget.IsDirty = true;

//Note this will skip the last column which is our timestamp
for(int i=0;i<silvTarget.Fields.Count-1;i++)
{
  if(!silvTarget.Fields.IsNull && !silvTarget.Fields.IsPrimaryKey)
  {
    silvTarget.Fields.IsChaged = true;
  }
}
  //Add record to the collection
  silvTableTarget.Add(silvTarget);
}