示例#1
0
        public string GetDataCompare(
            DataCompareConfiguration dataCompareConfiguration
            )
        {
            config = dataCompareConfiguration;
            var sourceDatabase = SqlSchema.GetDatabase(config.SourceServerName, config.SourceDatabaseName);
            var targetDatabase = SqlSchema.GetDatabase(config.TargetServerName, config.TargetDatabaseName);

            var returnScript = new StringBuilder();

            returnScript.AppendLine("-- Source Server Name: " + config.SourceServerName);
            returnScript.AppendLine("-- Source Database Name: " + config.SourceDatabaseName);
            returnScript.AppendLine("-- Target Server Name: " + config.TargetServerName);
            returnScript.AppendLine("-- Target Database Name: " + config.TargetDatabaseName);
            returnScript.AppendLine("-- Run Date: " + DateTime.Now.ToString());
            returnScript.AppendLine();
            returnScript.AppendLine("USE [" + config.TargetDatabaseName + "];");
            returnScript.AppendLine("GO");
            returnScript.AppendLine();

            returnScript.AppendLine(GetDataChanges(sourceDatabase));
            returnScript.AppendLine();
            returnScript.AppendLine("GO");

            return(returnScript.ToString());
        }
示例#2
0
        public string GetSchemaCompare(
            string sourceServerName,
            string sourceDatabaseName,
            string targetServerName,
            string targetDatabaseName
            )
        {
            var sourceDatabase = SqlSchema.GetDatabase(sourceServerName, sourceDatabaseName);
            var targetDatabase = SqlSchema.GetDatabase(targetServerName, targetDatabaseName);

            var returnScript = new StringBuilder();

            returnScript.AppendLine("-- Source Server Name: " + sourceServerName);
            returnScript.AppendLine("-- Source Database Name: " + sourceDatabaseName);
            returnScript.AppendLine("-- Target Server Name: " + targetServerName);
            returnScript.AppendLine("-- Target Database Name: " + targetDatabaseName);
            returnScript.AppendLine("-- Run Date: " + DateTime.Now.ToString());
            returnScript.AppendLine();
            returnScript.AppendLine("USE [" + targetDatabaseName + "];");
            returnScript.AppendLine("GO");
            returnScript.AppendLine();

            returnScript.AppendLine(GetDatabaseObjects(sourceDatabase, targetDatabase));

            return(returnScript.ToString());
        }
示例#3
0
        private string GetDatabaseObjects(
            Database sourceDatabase,
            Database targetDatabase
            )
        {
            var returnScript = new StringBuilder();

            returnScript.AppendLine(CompareSchemas(sourceDatabase.Schemas, targetDatabase.Schemas));

            List <CompareSQL> compareSqlList = new List <CompareSQL>();
            // Add objects in the order they should be created
            TableCompare tableCompare = new TableCompare();

            compareSqlList.Add(tableCompare.CompareTables(sourceDatabase.Tables, targetDatabase.Tables));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetUserDefinedTableTypes(sourceDatabase), SqlSchema.GetUserDefinedTableTypes(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetStoredProcedures(sourceDatabase), SqlSchema.GetStoredProcedures(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetViews(sourceDatabase), SqlSchema.GetViews(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetUserDefinedFunctions(sourceDatabase), SqlSchema.GetUserDefinedFunctions(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetTriggers(sourceDatabase), SqlSchema.GetTriggers(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetIndexes(sourceDatabase), SqlSchema.GetIndexes(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetPrimaryKeys(sourceDatabase), SqlSchema.GetPrimaryKeys(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetForeignKeys(sourceDatabase), SqlSchema.GetForeignKeys(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetDefaults(sourceDatabase), SqlSchema.GetDefaults(targetDatabase)));
            compareSqlList.Add(CompareDatabaseObjects(SqlSchema.GetChecks(sourceDatabase), SqlSchema.GetChecks(targetDatabase)));

            var dropScript   = new StringBuilder();
            var createScript = new StringBuilder();

            foreach (CompareSQL item in compareSqlList)
            {
                if (item.CreateSQL.Trim().Length > 0)
                {
                    createScript.AppendLine(item.CreateSQL.Trim());
                }
            }
            compareSqlList.Reverse();
            foreach (CompareSQL item in compareSqlList)
            {
                if (item.DropSQL.Trim().Length > 0)
                {
                    dropScript.AppendLine(item.DropSQL.Trim());
                }
            }

            returnScript.AppendLine("-- Drop Objects");
            returnScript.AppendLine(dropScript.ToString());
            returnScript.AppendLine("-- Create Objects");
            returnScript.AppendLine(createScript.ToString());

            return(returnScript.ToString());
        }