Advertisement
Guest User

Untitled

a guest
May 1st, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using Microsoft.SqlServer.Management.Smo;
  2.  
  3. This is the code which we can add as a method to any of the existing class file
  4.  
  5. Server oServer = new Server();
  6. try
  7.  
  8. {
  9.  
  10. oServer.ConnectionContext.ServerInstance = "TestInstance";
  11.  
  12. oServer.SetDefaultInitFields(typeof(StoredProcedure), "IsSystemObject");
  13.  
  14. oServer.ConnectionContext.LoginSecure = false;
  15.  
  16. oServer.ConnectionContext.Login = "sa";
  17.  
  18. oServer.ConnectionContext.Password = "password";
  19.  
  20. oServer.ConnectionContext.Connect();
  21.  
  22.  
  23. TextWriter oTextWriter = null;
  24.  
  25. StringBuilder oSBText = null;
  26.  
  27.  
  28. foreach (Database oDB in oServer.Databases)
  29.  
  30. {
  31.  
  32. if (oDB.Name.ToUpper().Equals("DB1"))
  33.  
  34. {
  35.  
  36. oTextWriter = new StreamWriter(@"D:\DBScripts\DbScripts1.sql");
  37.  
  38. }
  39.  
  40. else if (oDB.Name.ToUpper().Equals("DB2"))
  41.  
  42. {
  43.  
  44. oTextWriter = new StreamWriter(@"D:\DBScripts\DbScriptss.sql");
  45.  
  46. oSBText = new StringBuilder();
  47.  
  48. }
  49.  
  50. if (oTextWriter != null)
  51.  
  52. {
  53.  
  54. foreach (Table oTable in oDB.Tables)
  55.  
  56. {
  57.  
  58. TriggerCollection oTriggerCollect = oTable.Triggers;
  59.  
  60.  
  61. if (oTriggerCollect.Count > 0)
  62.  
  63. {
  64.  
  65. oSBText.Append("\n" + "--" + oDB.Name + "\n" + "--" + oTable.Name);
  66.  
  67.  
  68. foreach (Trigger oTrg in oTriggerCollect)
  69.  
  70. {
  71.  
  72. try
  73.  
  74. {
  75.  
  76. oSBText.Append("\n" + oTrg.TextHeader + "\n" + oTrg.TextBody);
  77.  
  78. }
  79.  
  80. catch (Exception Ex)
  81.  
  82. {
  83.  
  84. TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
  85.  
  86. oErrorWriter.Write(Ex.Message);
  87.  
  88. oErrorWriter.Close();
  89.  
  90. }
  91.  
  92. }
  93.  
  94. }
  95.  
  96. }
  97.  
  98.  
  99. foreach (StoredProcedure oSP in oDB.StoredProcedures)
  100.  
  101. {
  102.  
  103. try
  104.  
  105. {
  106.  
  107. if (oSP.IsSystemObject == false)
  108.  
  109. {
  110.  
  111. oSBText.Append("\n" + "--" + oDB.Name + "\n" + "--" + oSP.Name + "\n");
  112.  
  113.  
  114. StringCollection sc = oSP.Script();
  115.  
  116.  
  117. foreach (string s in sc)
  118.  
  119. oSBText.Append("\n" + s);
  120.  
  121. }
  122.  
  123. }
  124.  
  125. catch (Exception Ex)
  126.  
  127. {
  128.  
  129. TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
  130.  
  131. oErrorWriter.Write(Ex.Message);
  132.  
  133. oErrorWriter.Close();
  134.  
  135. }
  136.  
  137. }
  138.  
  139.  
  140. foreach (View oV in oDB.Views)
  141.  
  142. {
  143.  
  144. try
  145.  
  146. {
  147.  
  148. oSBText.Append("\n" + "--" + oDB.Name + "\n" + "--" + oV.Name + "\n");
  149.  
  150.  
  151. StringCollection sc = oV.Script();
  152.  
  153.  
  154. foreach (string s in sc)
  155.  
  156. oSBText.Append("\n" + s);
  157.  
  158. }
  159.  
  160. catch (Exception Ex)
  161.  
  162. {
  163.  
  164. TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
  165.  
  166. oErrorWriter.Write(Ex.Message);
  167.  
  168. oErrorWriter.Close();
  169.  
  170. }
  171.  
  172. }
  173.  
  174.  
  175. foreach (UserDefinedFunction oUDF in oDB.UserDefinedFunctions)
  176.  
  177. {
  178.  
  179. try
  180.  
  181. {
  182.  
  183. oSBText.Append("\n" + "--" + oDB.Name + "\n" + "--" + oUDF.Name + "\n");
  184.  
  185.  
  186. StringCollection sc = oUDF.Script();
  187.  
  188.  
  189. foreach (string s in sc)
  190.  
  191. oSBText.Append("\n" + s);
  192.  
  193. }
  194.  
  195. catch (Exception Ex)
  196.  
  197. {
  198.  
  199. TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
  200.  
  201. oErrorWriter.Write(Ex.Message);
  202.  
  203. oErrorWriter.Close();
  204.  
  205. }
  206.  
  207. }
  208.  
  209.  
  210. oTextWriter.Write(oSBText.ToString());
  211.  
  212. oTextWriter.Close();
  213.  
  214. oTextWriter = null;
  215.  
  216. oSBText = null;
  217.  
  218. }
  219.  
  220. }
  221.  
  222.  
  223. Console.WriteLine("Successfully Written to the file!!!");
  224.  
  225. }
  226.  
  227. catch (Exception Ex)
  228.  
  229. {
  230.  
  231. TextWriter oErrorWriter = new StreamWriter(@"D:\DBScripts\Error\Error.txt");
  232.  
  233. oErrorWriter.Write(Ex.Message);
  234.  
  235. oErrorWriter.Close();
  236.  
  237. }
  238.  
  239. finally
  240.  
  241. {
  242.  
  243. if (oServer.ConnectionContext.IsOpen)
  244.  
  245. {
  246.  
  247. oServer.ConnectionContext.Disconnect();
  248.  
  249. }
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement