This is a quick one and I see one issue already in the script generating a useless Go, use, go but here we go.

Using SMO to script some indexes and FK’s found on tables.

Create a new c# app project in VS.NET (mine is named object scripter). Add the GUI objects so it looks like this

Paste the code (not error handled and very quickly written..you’ve been warned!!!) below in the code view

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
 
namespace object_scripter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        public static string scripter(string scriptAction,string server, string dbname)
        {
            StringCollection sc = new StringCollection();
            ScriptingOptions so = new ScriptingOptions();
            so.IncludeDatabaseContext = true;
            so.DriForeignKeys = true;
 
            StoredProcedure sp = new StoredProcedure();
            Server serv = new Server(server);
            Database dbase = serv.Databases[dbname];
 
            string script = "";
 
            foreach (Table t in dbase.Tables)
            {
                foreach (ForeignKey key in t.ForeignKeys) 
                {
                        sc = key.Script(so);
                        foreach (string s in sc)
                        {
                            script += "nrGonr" + s;
                        }
                }
            }
 
            sc.Clear();
 
            foreach (Table t in dbase.Tables)
            {
                foreach (Index index in t.Indexes)
                {
                    sc = index.Script(so);
                    foreach (string s in sc)
                    {
                        script += "nrGonr" + s;
                    }
                }
            }
 
            return script;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText(scripter("CREATE", textBox1.Text, textBox2.Text));
        }
    }
}
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;

namespace object_scripter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        public static string scripter(string scriptAction,string server, string dbname)
        {
            StringCollection sc = new StringCollection();
            ScriptingOptions so = new ScriptingOptions();
            so.IncludeDatabaseContext = true;
            so.DriForeignKeys = true;

            StoredProcedure sp = new StoredProcedure();
            Server serv = new Server(server);
            Database dbase = serv.Databases[dbname];

            string script = "";

            foreach (Table t in dbase.Tables)
            {
                foreach (ForeignKey key in t.ForeignKeys) 
                {
                        sc = key.Script(so);
                        foreach (string s in sc)
                        {
                            script += "nrGonr" + s;
                        }
                }
            }

            sc.Clear();

            foreach (Table t in dbase.Tables)
            {
                foreach (Index index in t.Indexes)
                {
                    sc = index.Script(so);
                    foreach (string s in sc)
                    {
                        script += "nrGonr" + s;
                    }
                }
            }

            return script;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText(scripter("CREATE", textBox1.Text, textBox2.Text));
        }
    }
}

Save and run. Enter an instance (dev one!!!), DB name and hit Run Me. Should appear as