Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.SqlServer.Management.Smo;
- This is the code which we can add as a method to any of the existing class file
- Server oServer = new Server();
- try
- {
- oServer.ConnectionContext.ServerInstance = "TestInstance";
- oServer.SetDefaultInitFields(typeof(StoredProcedure), "IsSystemObject");
- oServer.ConnectionContext.LoginSecure = false;
- oServer.ConnectionContext.Login = "sa";
- oServer.ConnectionContext.Password = "password";
- oServer.ConnectionContext.Connect();
- TextWriter oTextWriter = null;
- StringBuilder oSBText = null;
- foreach (Database oDB in oServer.Databases)
- {
- if (oDB.Name.ToUpper().Equals("DB1"))
- {
- oTextWriter = new StreamWriter(@"D:\DBScripts\DbScripts1.sql");
- }
- else if (oDB.Name.ToUpper().Equals("DB2"))
- {
- oTextWriter = new StreamWriter(@"D:\DBScripts\DbScriptss.sql");
- oSBText = new StringBuilder();
- }
- if (oTextWriter != null)
- {
- foreach (Table oTable in oDB.Tables)
- {
- TriggerCollection oTriggerCollect = oTable.Triggers;
- if (oTriggerCollect.Count > 0)
- {
- oSBText.Append("\n" + "--" + oDB.Name + "\n" + "--" + oTable.Name);
- foreach (Trigger oTrg in oTriggerCollect)
- {
- try
- {
- oSBText.Append("\n" + oTrg.TextHeader + "\n" + oTrg.TextBody);
- }
- catch (Exception Ex)
- {
- TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
- oErrorWriter.Write(Ex.Message);
- oErrorWriter.Close();
- }
- }
- }
- }
- foreach (StoredProcedure oSP in oDB.StoredProcedures)
- {
- try
- {
- if (oSP.IsSystemObject == false)
- {
- oSBText.Append("\n" + "--" + oDB.Name + "\n" + "--" + oSP.Name + "\n");
- StringCollection sc = oSP.Script();
- foreach (string s in sc)
- oSBText.Append("\n" + s);
- }
- }
- catch (Exception Ex)
- {
- TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
- oErrorWriter.Write(Ex.Message);
- oErrorWriter.Close();
- }
- }
- foreach (View oV in oDB.Views)
- {
- try
- {
- oSBText.Append("\n" + "--" + oDB.Name + "\n" + "--" + oV.Name + "\n");
- StringCollection sc = oV.Script();
- foreach (string s in sc)
- oSBText.Append("\n" + s);
- }
- catch (Exception Ex)
- {
- TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
- oErrorWriter.Write(Ex.Message);
- oErrorWriter.Close();
- }
- }
- foreach (UserDefinedFunction oUDF in oDB.UserDefinedFunctions)
- {
- try
- {
- oSBText.Append("\n" + "--" + oDB.Name + "\n" + "--" + oUDF.Name + "\n");
- StringCollection sc = oUDF.Script();
- foreach (string s in sc)
- oSBText.Append("\n" + s);
- }
- catch (Exception Ex)
- {
- TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
- oErrorWriter.Write(Ex.Message);
- oErrorWriter.Close();
- }
- }
- oTextWriter.Write(oSBText.ToString());
- oTextWriter.Close();
- oTextWriter = null;
- oSBText = null;
- }
- }
- Console.WriteLine("Successfully Written to the file!!!");
- }
- catch (Exception Ex)
- {
- TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
- oErrorWriter.Write(Ex.Message);
- oErrorWriter.Close();
- }
- finally
- {
- if (oServer.ConnectionContext.IsOpen)
- {
- oServer.ConnectionContext.Disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement