private void cmdExecute_Click(object sender, EventArgs e) { try { ScriptMove move = new ScriptMove(); move.ServerName = txtServer.Text; move.DatabaseName = txtDatabase.Text; move.UserId = txtUser.Text; move.Password = txtPassword.Text; move.UseIntegratedAuthentication = chkUseIntegratedAuthentication.Checked; move.MatchOnNameContains = chkFilterOnNameContaining.Checked; move.TextToMatchOnNameContains = txtNameContains.Text; move.IncludeTables = chkIncludeTables.Checked; move.IncludeViews = chkIncludeViews.Checked; move.IncludeProcedures = chkIncludeProcedures.Checked; move.IncludeTriggers = chkIncludeTriggers.Checked; move.IncludeDDLTriggers = chkIncludeDDLTriggers.Checked; move.IncludeFunctions = chkIncludeFunctions.Checked; move.Execute( txtScriptDir.Text ); MessageBox.Show(this, "Script Export Complete."); } catch (Exception ex) { MessageBox.Show(this, string.Format("An exception occurred: {0}"), ex.Message); } }
public static void ScriptTables( Database database, string scriptDirectory, Microsoft.SqlServer.Management.Smo.Scripter scripter, bool matchOnNameContains, string textToMatchOnNameContains) { string tableDirectory = Path.Combine(scriptDirectory, "tables"); System.IO.Directory.CreateDirectory(tableDirectory); Stopwatch blockStart = new Stopwatch(); blockStart.Start(); TableCollection allTables = database.Tables; List <Microsoft.SqlServer.Management.Sdk.Sfc.Urn> allTableObjects = new List <Microsoft.SqlServer.Management.Sdk.Sfc.Urn>(); int tableIndex = 0; ScriptMove.WriteToLog("Scripting tables..."); foreach (Table oneTable in database.Tables) { if (!oneTable.IsSystemObject) { if (matchOnNameContains == false || (matchOnNameContains == true && oneTable.Name.ToUpper().Contains(textToMatchOnNameContains))) { List <Microsoft.SqlServer.Management.Sdk.Sfc.Urn> oneTableObject = new List <Microsoft.SqlServer.Management.Sdk.Sfc.Urn>(); SqlSmoObject[] objectArray = { oneTable }; int depCount = CountTableDependancies(scripter, objectArray); if (depCount > 0) { ScriptMove.WriteToLog(string.Format("table {0} has {1} dependancies", oneTable.Name, depCount)); } oneTableObject.Add(oneTable.Urn); allTableObjects.Add(oneTable.Urn); string fileName = string.Format("{0}.{1}.{2}.sql", string.Format("0000{0}", tableIndex).Right(4), oneTable.Schema, oneTable.Name); string fullFileName = Path.Combine(tableDirectory, fileName); try { WriteScriptToFile(fullFileName, oneTable.Urn, ref scripter); } catch (Exception ex) { ScriptMove.WriteToLog(String.Format(" Unable to script {0} due to error {1}", oneTable.Name, ex.Message)); } tableIndex++; } } } ScriptMove.WriteToLog(String.Format("{0} tables scripted. Elapsed seconds: {1}", tableIndex, blockStart.Elapsed.TotalSeconds)); }
public static void ScriptDatabaseTriggers( Database database, string scriptDirectory, Microsoft.SqlServer.Management.Smo.Scripter scripter, bool matchOnNameContains, string textToMatchOnNameContains) { string triggerDirectory = Path.Combine(scriptDirectory, "ddltriggers"); System.IO.Directory.CreateDirectory(triggerDirectory); Stopwatch blockStart = new Stopwatch(); blockStart.Start(); DatabaseDdlTriggerCollection allTriggers = database.Triggers; List <Microsoft.SqlServer.Management.Sdk.Sfc.Urn> allTriggerObjects = new List <Microsoft.SqlServer.Management.Sdk.Sfc.Urn>(); int triggerIndex = 0; ScriptMove.WriteToLog("Scripting DDL triggers..."); foreach (DatabaseDdlTrigger oneTrigger in allTriggers) { if (!oneTrigger.IsSystemObject) { if (matchOnNameContains == false || (matchOnNameContains == true && oneTrigger.Name.ToUpper().Contains(textToMatchOnNameContains))) { List <Microsoft.SqlServer.Management.Sdk.Sfc.Urn> oneTriggerObject = new List <Microsoft.SqlServer.Management.Sdk.Sfc.Urn>(); oneTriggerObject.Add(oneTrigger.Urn); string fileName = string.Format("{0}.{1}.{2}.sql", string.Format("0000{0}", triggerIndex).Right(4), "dbo", oneTrigger.Name); string fullFileName = Path.Combine(triggerDirectory, fileName); try { WriteScriptToFile(fullFileName, oneTrigger.Urn, ref scripter); } catch (Exception ex) { ScriptMove.WriteToLog(String.Format(" Unable to script {0} due to error {1}", oneTrigger.Name, ex.Message)); } triggerIndex++; } } } ScriptMove.WriteToLog(String.Format("{0} DDL triggers scripted. Elapsed seconds: {1}", triggerIndex, blockStart.Elapsed.TotalSeconds)); }