using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace ScriptExistingTable
{
    class Program
    {
        static void Main(string[] args)
        {
            Server local = new Server("CANOPUS5");
            Table customer = (Table)
              local.GetSmoObject(@"Server
                /Database[@Name='pubs']
                /Table[@Name='Titles' and @Schema='dbo']");
            StringCollection script = customer.Script();
            Console.WriteLine("\nAdd table");
            foreach (String s in script)
            {
                Console.WriteLine(s);
            }
           
            ScriptingOptions sopts = new ScriptingOptions();
            sopts.ScriptDrops=true;
            script = customer.Script(sopts);
            Console.WriteLine("\nDrop table");
            foreach (String s in script)
            {
                Console.WriteLine(s);
            }
            sopts.IncludeDatabaseContext = true;
            sopts.IncludeHeaders = true;
            sopts.WithDependencies = true;
            sopts.ScriptDrops = false;
            sopts.SchemaQualify = true;
            script = customer.Script(sopts);
            Console.WriteLine("\nAdd table with dep");
            foreach (String s in script)
            {
                Console.WriteLine(s);
            }
            sopts.Indexes = true;
            sopts.DriPrimaryKey = true;
            sopts.DriForeignKeys = true;
            Console.WriteLine("\nAdd table with indexes");
            script = customer.Script(sopts);
            foreach (String s in script)
            {
                Console.WriteLine(s);
            }



        }
    }
}
