MSSQLのプログラミングスクリプト生成

頻繁かつ無秩序なデータベース変更のために、多くのユーザーが変更の履歴についてよく質問します。 これは、日中にデータベースで発生したすべての変更を完全に記録することではありません。 興味深いのは、稼働日の終了後の毎日のデータベース構造の画像です。 SQL Server Management Studioを使用すると、スクリプトを個別にまたは一度に生成できます。 .NETアプリケーションでSQL Server Management Studioのライブラリセットを使用することにより、完全なアクションの自由を得ることができます。 スクリプト生成プログラムの説明:以下の表、ビュー、手順。



図書館の添付


名前空間のクラスが必要です:

using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Sdk.Sfc;
      
      





それらを含むライブラリは同じ名前を持ち、フォルダにあります:

C:\ Program Files \ Microsoft SQL Server \ 90 \ SDK \アセンブリ

または

C:\ Program Files \ Microsoft SQL Server \ 100 \ SDK \アセンブリ

SQL Serverのバージョンに応じて。



実行中のスクリプト


スクリプトジェネレーターは、指定したディレクトリに個別のテーブル、ビュー、プロシージャフォルダーを作成します。 オブジェクトを作成するためのスクリプトを作成し、適切なフォルダー内の個別のファイルに保存します。 テーブルの場合、依存関係(キー、インデックスなど)を考慮して、または考慮せずに生成が実行されます。 指定されたディレクトリに同じタイプのスクリプトの共通ファイルを作成します。



 //   Server myServer = new Server(@"myServ"); // Windows myServer.ConnectionContext.LoginSecure = true; //  myServer.ConnectionContext.Connect(); //  ,       string dir = Application.StartupPath +@"\"+ DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString(); Directory.CreateDirectory(dir); // ,   GenerateTableScript(myServer,dir); // ,   GenerateProceduresScript(myServer, dir); // ,   GenerateViewScript(myServer, dir); //  myServer.ConnectionContext.Disconnect();
      
      





テーブルのスクリプト生成


  private static void GenerateTableScript(Server myServer, string path) { Directory.CreateDirectory(path + @"\Tables\"); string text = ""; string textWithDependencies = ""; //  ,     Scripter scripter = new Scripter(myServer); //    , "ZZZ" -    Database myAdventureWorks = myServer.Databases["ZZZ"]; //      ScriptingOptions scriptOptions = new ScriptingOptions(); //        //    Drop scriptOptions.ScriptDrops = false; //    If Not Exists scriptOptions.IncludeIfNotExists = false; //   foreach (Table myTable in myAdventureWorks.Tables) { // sql      StringCollection tableScripts = myTable.Script(scriptOptions); //    string newSql = ""; //  foreach (string script in tableScripts) { newSql = newSql + script; text = text + script; } //        File.WriteAllText(path + @"\Tables\" + myTable.Name + ".sql", newSql); //    scriptOptions.DriAllConstraints = true; scriptOptions.DriAllKeys = true; scriptOptions.DriDefaults = true; scriptOptions.DriForeignKeys = true; scriptOptions.DriIndexes = true; scriptOptions.DriNonClustered = true; scriptOptions.DriPrimaryKey = true; scriptOptions.DriUniqueKeys = true; tableScripts = myTable.Script(scriptOptions); newSql = ""; foreach (string script in tableScripts) { newSql = newSql + script; textWithDependencies = text + script; } //        File.WriteAllText(path + @"\Tables\" + myTable.Name + "_WithDependencies.sql", newSql); } //    File.WriteAllText(path + @"\" + "AllTable_WithDependencies.sql", text); File.WriteAllText(path + @"\" + "AllTable.sql", text); }
      
      





ビューのスクリプトを生成する


  private static void GenerateViewScript(Server myServer, string path) { Directory.CreateDirectory(path + @"\View\"); string text = ""; Scripter scripter = new Scripter(myServer); Database myAdventureWorks = myServer.Databases["ZZZ"]; ScriptingOptions scriptOptions = new ScriptingOptions(); scriptOptions.ScriptDrops = false; scriptOptions.IncludeIfNotExists = false; foreach (Microsoft.SqlServer.Management.Smo.View myView in myAdventureWorks.Views) { StringCollection ProcedureScripts = myView.Script(scriptOptions); ProcedureScripts = myView.Script(); string newSql = ""; foreach (string script in ProcedureScripts) { newSql = newSql + script; text = text + script; } File.WriteAllText(path + @"\Views\" + myView.Name + ".sql", newSql); } File.WriteAllText(path + @"\" + "AllView.sql", text); }
      
      





プロシージャのスクリプト生成


  private static void GenerateProceduresScript(Server myServer, string path) { Directory.CreateDirectory(path + @"\Procedures\"); string text = ""; Scripter scripter = new Scripter(myServer); Database myAdventureWorks = myServer.Databases["ZZZ"]; ScriptingOptions scriptOptions = new ScriptingOptions(); scriptOptions.ScriptDrops = false; scriptOptions.IncludeIfNotExists = false; foreach (StoredProcedure myProcedure in myAdventureWorks.StoredProcedures) { StringCollection ProcedureScripts = myProcedure.Script(scriptOptions); ProcedureScripts = myProcedure.Script(); string newSql = ""; foreach (string script in ProcedureScripts) { newSql = newSql + script; text = text + script; } File.WriteAllText(path + @"\Procedures\" + myProcedure.Name + ".sql", newSql); } File.WriteAllText(path + @"\" + "AllProcedure.sql", text); }
      
      





おわりに


説明されている機能は、データベース構造の変更のロギング、ページ化されたオブジェクトの名前による自動フィルタリングに役立ちます。 たとえば、名前の接頭辞は、プロジェクトの別の方向を示している場合があります。 一般および個別のスクリプトをSVNに追加できます。 データベースオブジェクトの自動比較を記述し、変更の事実に基づいて責任者にメッセージを送信できます。

コードはここにあります

資料の基礎は次の記事でした: SQL ServerのSMOを使用したデータベースオブジェクトのスクリプトの生成



All Articles