Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Server 2008 Developer Training Kit Available For DownloadStop mirroring for server reboot »
    comments

    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

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

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

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt
    Social SitingsTwitterLinkedInLTD RSS Feed
    2677 views
    Instapaper

    1 comment

    Comment from: David G. [Visitor]
    David G. One issue I've found with SMO is that if your tables are contained in a schema, the foreign keys REFERENCES-clause will be missing the schema on the referenced table.

    Otherwise, it works great.
    09/28/10 @ 16:38

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)