Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于哈希的SQL server DB模式脚本生成的最快方法_Sql_Sql Server_Schema - Fatal编程技术网

用于哈希的SQL server DB模式脚本生成的最快方法

用于哈希的SQL server DB模式脚本生成的最快方法,sql,sql-server,schema,Sql,Sql Server,Schema,我想将整个SQL模式转换为DB,然后生成它的哈希。这样我就可以检查回滚脚本是否将模式返回到其原始状态。是否有一个SP我可以使用或其他一些狡猾的方法?我希望它尽可能快。我编写了一个名为的工具,它使用SMO库调用为数据库中的所有对象编写脚本。您可以使用它创建一个.sql文件,并找到另一个工具来计算结果文件上的哈希值。(例如,随机google提供)如果将表和键与代码和约束分开,则可以轻松地对后者进行散列 SELECT CHECKSUM_AGG(BINARY_CHECKSUM (*)) FROM

我想将整个SQL模式转换为DB,然后生成它的哈希。这样我就可以检查回滚脚本是否将模式返回到其原始状态。是否有一个SP我可以使用或其他一些狡猾的方法?我希望它尽可能快。

我编写了一个名为的工具,它使用SMO库调用为数据库中的所有对象编写脚本。您可以使用它创建一个.sql文件,并找到另一个工具来计算结果文件上的哈希值。(例如,随机google提供)

如果将表和键与代码和约束分开,则可以轻松地对后者进行散列

SELECT
    CHECKSUM_AGG(BINARY_CHECKSUM (*))
FROM   
    (SELECT
        definition 
    FROM
        sys.default_constraints
    UNION ALL
    SELECT
        definition 
    FROM
        sys.sql_modules
    UNION ALL
    SELECT
        definition 
    FROM
        sys.check_constraints
    ) foo

以下方面应起作用:

        Microsoft.SqlServer.Management.Smo.Server srv = new Microsoft.SqlServer.Management.Smo.Server("Server");

        Microsoft.SqlServer.Management.Smo.Database db = srv.Databases["DB_Name"];

        // Set scripting options as needed using a ScriptingOptions object.
        Microsoft.SqlServer.Management.Smo.ScriptingOptions so = new ScriptingOptions();
        so.AllowSystemObjects = false;
        so.ScriptDrops = false;
        so.Indexes = true;
        so.ClusteredIndexes = true;
        so.PrimaryObject = true;
        so.SchemaQualify = true;
        so.IncludeIfNotExists = false;
        so.Triggers = true;

        System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
        StringBuilder sb = new StringBuilder();

        foreach (Table item in db.Tables)
            if (!item.IsSystemObject)
            {
                sc = item.Script(so);
                foreach (string s in sc)
                    sb.Append(s);
            }

        foreach (StoredProcedure item in db.StoredProcedures)
            if (!item.IsSystemObject)
                if (!item.IsSystemObject)
                {
                    sc = item.Script(so);
                    foreach (string s in sc)
                        sb.Append(s);
                }

        foreach (UserDefinedFunction item in db.UserDefinedFunctions)
            if (!item.IsSystemObject)
                if (!item.IsSystemObject)
                {
                    sc = item.Script(so);
                    foreach (string s in sc)
                        sb.Append(s);
                }

        foreach (Trigger item in db.Triggers)
            if (!item.IsSystemObject)
                if (!item.IsSystemObject)
                {
                    sc = item.Script(so);
                    foreach (string s in sc)
                        sb.Append(s);
                }


        //sb.GetHashCode();
        // For a better hash do this.
        System.Security.Cryptography.MD5CryptoServiceProvider hashProvider = new System.Security.Cryptography.MD5CryptoServiceProvider();

        byte[] hashData = hashProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sb.ToString()));

1:2005/2008,2:Nope,3:V2:)我需要表的散列,存储过程,约束,我担心的一切!那么一个不完整的答案是-1?既然你已经写了一个工具,为什么要问这个问题?对不起,Matt,刚刚在LinqPad中试用过。这似乎是为数据库本身编写脚本,而不是为表、模式或其他对象编写脚本。另外,我认为sb.GetHashCode()只是一个适用于当前AppDomain的哈希值!它返回stringbuilder的内存位置,而不是其值的散列,因此下次运行应用程序时,代码将不同。新建StringBuilder(“你好”).GetHashCode()!=新建StringBuilder(“Hello”).GetHashCode()您是对的。我以前也犯过同样的错误。也许这次我学会了。我已经编辑了代码。希望这更接近。仍然存在空白的问题,并确定到底需要什么样的细节,但这应该更接近。还不错。SMO对我来说有点慢。可能会使用OpenDbDiff做一些类似的事情,因为似乎没有人可以使用一个SP调用。