What are the challenges you face when working across database platforms? Take the survey

Scripting against an empty folder

dmorydmory Posts: 5
I'm trying to do a comparison between two script folders, one with valid SQL scripts and a temporary one which is empty, so that I can merge all the script files of the first folder into a single script. When I try to do this, it does not script any key constraints (primary keys, foreign keys, etc).
private string CreateScript(string sScriptPath)
            string sScript = "";
            string sTempFolder = CreateTemporaryFolder();
            Database dbSource = new Database();
            Database dbEmpty = new Database();
            ReadFromScriptDatabaseInformation r = new ReadFromScriptDatabaseInformation();
            r.SQLServerDBVersion = RedGate.Shared.SQL.Server.SQLVersion.SqlServer2005;
            r.DefaultCollation = "SQL_Latin1_General_CP1_CI_AS";                
            dbSource.Register(sScriptPath, r, Options.Default);
            dbEmpty.Register(sTempFolder, r, Options.Default);
            Differences diffs = dbSource.CompareWith(dbEmpty, Options.Default);
            foreach (Difference diff in diffs)
                diff.Selected = true;

            Work w = new Work();
            w.BuildFromDifferences(diffs, Options.Default, true);
            sScript = w.ExecutionBlock.GetString();
            return sScript;

        private string CreateTemporaryFolder()
            Random r = new Random();            
            string sPath = "";
            DirectoryInfo di = null;

            int nNumAttempts = 0;
            while (di == null)
                sPath = Path.GetTempPath() + "_" + r.Next(0, 1000000).ToString();
                    di = Directory.CreateDirectory(sPath);
                    if (nNumAttempts >= 50)
            return sPath;
Have I coded this correctly? Is it even possible to do this?

Any help would be much appreciated.


  • Options
    Michelle TMichelle T Posts: 566 Gold 1
    If you're trying to produce a synchronization script from a comparison between two script folders, you'll need to specify the option Options.ForceSyncScriptGeneration (just change Options.Default to Options.Default | Options.ForceSyncScriptGeneration). Otherwise SQL Compare doesn't bother to generate a full sync script because it doesn't actually need one to synchronize the two script folders.
    Software Developer
    Redgate Software
  • Options
    It looks like that worked. Thank you.
Sign In or Register to comment.