title: How to iterate through database objects in C# using SMO
body: <p>I am trying to backup my database objects' scripts to disk. I have been able to put together the below code that does this, but I have to specify each object type (eg Tables, Functions, Stored Procedures). </p>
  
  <p>Is there a way to loop through all these objects without specifying each collection?</p>
  
  <pre><code>using System;
  using System.Data;
  using Microsoft.SqlServer.Management.Smo;
  using Microsoft.SqlServer.Management.Common;
  using System.IO;
  using System.Text;
  
  public static void BackupDB(Server srv, Database db, string savePath)
  {
      string objPath;
      string objFile;
      string objTxt;
  
      foreach (StoredProcedure obj in db.StoredProcedures) 
      {
          if (obj.IsSystemObject == false)
          {
              objPath = savePath + "Stored Procedures\\";
              Directory.CreateDirectory(objPath);
  
              objFile = objPath + obj.Schema + "." + obj.Name + ".sql";
              objTxt = GetScriptString(srv, obj);
              File.WriteAllText(objFile, objTxt);
          }
      }
  
      foreach (Table obj in db.Tables)
      {
          if (obj.IsSystemObject == false)
          {
              objPath = savePath + "Tables\\";
              Directory.CreateDirectory(objPath);
  
              objFile = objPath + obj.Schema + "." + obj.Name + ".txt";
              objTxt = GetScriptString(srv, obj);
              File.WriteAllText(objFile, objTxt);
          }
      }
  
  static private string GetScriptString(Server server, SqlSmoObject obj)
  {
      StringBuilder output = new StringBuilder();
      Scripter scr = new Scripter(server);
      var script = scr.EnumScript(new SqlSmoObject[] { obj });
      foreach (var line in script)
      {
          output.AppendLine(line);
      }
      return output.ToString();
  }
  </code></pre>
  
  <p>EDIT:
  Using Grant Winney's answer, I was able to put together the below method to get iterate through all the objects without repeating lines:</p>
  
  <pre><code>public static void BackupDB(Server srv, Database db, string savePath)
  {
      string objPath;
      string objFile;
      string objTxt;
  
      Dictionary&lt;string, IEnumerable&lt;ScriptSchemaObjectBase&gt;&gt; dboDict =
          new Dictionary&lt;string, IEnumerable&lt;ScriptSchemaObjectBase&gt;&gt;();
  
      dboDict.Add("Stored Procedures", db.StoredProcedures.Cast&lt;StoredProcedure&gt;().Where(x =&gt; !x.IsSystemObject));
      dboDict.Add("Functions", db.UserDefinedFunctions.Cast&lt;UserDefinedFunction&gt;().Where(x =&gt; !x.IsSystemObject));
      dboDict.Add("Tables", db.Tables.Cast&lt;Table&gt;().Where(x =&gt; !x.IsSystemObject));
      dboDict.Add("Views", db.Views.Cast&lt;View&gt;().Where(x =&gt; !x.IsSystemObject));
  
      foreach (KeyValuePair&lt;string, IEnumerable&lt;ScriptSchemaObjectBase&gt;&gt; dict in dboDict)
      {
          IEnumerable&lt;ScriptSchemaObjectBase&gt; dboObjects = dict.Value;
          objPath = savePath + dict.Key + "\\";
          Directory.CreateDirectory(objPath);
  
          foreach (ScriptSchemaObjectBase obj in dboObjects)
          {
              objFile = objPath + obj.Schema + "." + obj.Name + ".sql";
              objTxt = GetScriptString(srv, obj);
              File.WriteAllText(objFile, objTxt);
          }
      }
  
  }
  </code></pre>