2009-05-27 39 views
3

,然後生成它的哈希值。這樣可以檢查回滾腳本是否將模式返回到原始狀態。有沒有我可以使用的SP或其他狡猾的方法?我希望它儘可能快。最快的方式進行散列

+0

1:2005/2008, 2:不, 3:多數民衆贊成V2 :) – mcintyre321 2009-05-27 22:50:54

回答

2

下面應該工作:

 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())); 
0

我寫了一個叫SMOscript工具使用SMO庫調用腳本的所有對象在數據庫中。您可以使用它來創建一個.sql文件,並找到另一個工具來計算結果文件上的散列。 (隨機谷歌可以調出this例如)

+0

不壞。 SMO對我的喜歡有點慢。可能會使用OpenDbDiff做類似的事情,因爲似乎沒有人有我可以使用的單個SP調用。 – mcintyre321 2009-05-27 23:05:54

2

如果單獨的表,並從代碼和約束密鑰,然後可以很容易地散列後者。

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 
+0

我需要表格,sprocs,約束的一切,我害怕! – mcintyre321 2009-05-27 22:53:07

+0

所以一個不完整的答案是-1?鑑於你已經寫了一個工具http://stackoverflow.com/questions/6371/how-do-you-manage-databases-in-development-test-and-production/541419#541419,你爲什麼問這個問題? – gbn 2009-05-28 06:24:27