using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Sdk.Sfc;
// 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); }
Source: https://habr.com/ru/post/145263/
All Articles