dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Custom Sync Tool

  1. #1
    Join Date
    Mar 2013
    Posts
    27

    Custom Sync Tool

    Hi

    How I can create one application that get results from one MS SQL Server query
    compare them with one MYSQL Server query and if it is equal then do update else do insert.

    For example, let say I have my source sql query
    Select ID, FName, LName From TBNames
    Results:
    ID | FName | Lname
    1 | John | Eaglin
    2 | Harry | Baggerly
    3 | Charlie | Abbett
    4 | George | Dent

    Then from Mysql query I have the result
    Select ID, fName, LName from MYNames where ID = {MSSQL_ID};
    ID | FName | Lname
    1 | John | Smith
    2 | Tomas | Ford

    I need to pass through ID field and update ID 1 and 2 from Smith to Eaglin
    from Tomas Ford to Harry Baggerly and insert the other 2 records.
    Running the same select on MYSQL I get the result
    ID | FName | Lname
    1 | John | Eaglin <-- Last Name changed
    2 | Harry | Baggerly <-- Both Names changed
    3 | Charlie | Abbett <-- Inserted
    4 | George | Dent <-- Inserted
    The problem is that queries are many with lot of different fields and tables.
    Is there any way I can pass fields names and results as variables to the update or insert query?

    Thank you
    Last edited by makis_best; November 27th, 2018 at 03:57 AM.

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,319

    Re: Custom Sync Tool

    You should be able to do this with just queries, update table one from table two under whatever conditions and insert into table one from table two under whatever conditions. Hard to be more specific with only the info provided.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Dec 2018
    Posts
    15

    Re: Custom Sync Tool

    You may try this code:

    Code:
    public class FileSyncProviderSample
    {
        public static void Main(string[] args)
        {
            if (args.Length < 2 ||
                string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]) ||
                !Directory.Exists(args[0]) || !Directory.Exists(args[1]))
            {
                Console.WriteLine(
                  "Usage: FileSyncSample [valid directory path 1] [valid directory path 2]");
                return;
            }
    
            string replica1RootPath = args[0];
            string replica2RootPath = args[1];
    
            try
            {
                // Set options for the synchronization operation
                FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges |
                    FileSyncOptions.RecycleDeletedFiles |
                    FileSyncOptions.RecyclePreviousFileOnUpdates |
                    FileSyncOptions.RecycleConflictLoserFiles;
    
                FileSyncScopeFilter filter = new FileSyncScopeFilter();
                filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files
    
                // Explicitly detect changes on both replicas upfront, to avoid two change
                // detection passes for the two-way synchronization
    
                DetectChangesOnFileSystemReplica(
                        replica1RootPath, filter, options);
                DetectChangesOnFileSystemReplica(
                    replica2RootPath, filter, options);
    
                // Synchronization in both directions
                SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options);
                SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nException from File Synchronization Provider:\n" + e.ToString());
            }
        }
    
        public static void DetectChangesOnFileSystemReplica(
                string replicaRootPath,
                FileSyncScopeFilter filter, FileSyncOptions options)
        {
            FileSyncProvider provider = null;
    
            try
            {
                provider = new FileSyncProvider(replicaRootPath, replicaRootPath, filter, options);
                provider.DetectChanges();
            }
            finally
            {
                // Release resources
                if (provider != null)
                    provider.Dispose();
            }
        }
    
        public static void SyncFileSystemReplicasOneWay(
                string sourceReplicaRootPath, string destinationReplicaRootPath,
                FileSyncScopeFilter filter, FileSyncOptions options)
        {
            FileSyncProvider sourceProvider = null;
            FileSyncProvider destinationProvider = null;
    
            try
            {
                sourceProvider = new FileSyncProvider(
                    sourceReplicaRootPath, filter, options);
                destinationProvider = new FileSyncProvider(
                    destinationReplicaRootPath, filter, options);
    
                destinationProvider.AppliedChange +=
                    new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
                destinationProvider.SkippedChange +=
                    new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);
    
                SyncOrchestrator agent = new SyncOrchestrator();
                agent.LocalProvider = sourceProvider;
                agent.RemoteProvider = destinationProvider;
                agent.Direction = SyncDirection.Upload; // Sync source to destination
    
                Console.WriteLine("Synchronizing changes to replica: " +
                    destinationProvider.RootDirectoryPath);
                agent.Synchronize();
            }
            finally
            {
                // Release resources
                if (sourceProvider != null) sourceProvider.Dispose();
                if (destinationProvider != null) destinationProvider.Dispose();
            }
        }
    
        public static void OnAppliedChange(object sender, AppliedChangeEventArgs args)
        {
            switch (args.ChangeType)
            {
                case ChangeType.Create:
                    Console.WriteLine("-- Applied CREATE for file " + args.NewFilePath);
                    break;
                case ChangeType.Delete:
                    Console.WriteLine("-- Applied DELETE for file " + args.OldFilePath);
                    break;
                case ChangeType.Overwrite:
                    Console.WriteLine("-- Applied OVERWRITE for file " + args.OldFilePath);
                    break;
                case ChangeType.Rename:
                    Console.WriteLine("-- Applied RENAME for file " + args.OldFilePath +
                                      " as " + args.NewFilePath);
                    break;
            }
        }
    
        public static void OnSkippedChange(object sender, SkippedChangeEventArgs args)
        {
            Console.WriteLine("-- Skipped applying " + args.ChangeType.ToString().ToUpper()
                  + " for " + (!string.IsNullOrEmpty(args.CurrentFilePath) ?
                                args.CurrentFilePath : args.NewFilePath) + " due to error");
    
            if (args.Exception != null)
                Console.WriteLine("   [" + args.Exception.Message + "]");
        }
    
    }
    Last edited by 2kaud; December 29th, 2018 at 04:01 AM. Reason: Added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)