SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Idera SQL Check

Generating SQL Server Scripts using SMO

This video show you how to generate SQL Server scripts using SMO

Duration:
3 mins 6 secs
Skill Level:
200
Rating:
4.38 out of 5
Publish Date:
September 09, 2008
Generating SQL Server Scripts using SMO You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Tim Mitchell
Tim Mitchell is a Microsoft SQL Server database developer, business intelligence consultant, writer, and speaker. He has been working professionally with SQL Server for over six years, and holds the MCTS and MCDBA certifications from Microsoft as well as a Bachelor's degree in computer science from Texas A&M University - Commerce.

References



Comments
ben on 10/9/2008
really nice - but where can I copy the code example?

Manish on 11/14/2008
This is really very help ful, but when I execute the generated script, I am getting error of some depending tables which are later in the list but their references were on the top. Need to do some modifications with the DependencyTree, DependencyWalker and DependencyCollection object of .net framework.

Saritha on 11/14/2008
Very Use full

Dale Moore on 11/14/2008
Very quick and simple for programmatically getting the objects from the database.

ben on 3/14/2009
great code/lesson

varma on 6/11/2009
Gud work.. it works grt.. I tried using the code Once it gets the scripts I want to backup and restore the database too I tried it as below.. its getting the scripts but not backing the database its not even prompting me any errors here i am executing this program from a remote server that should not be a problem right.. Thanks.. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Collections.Specialized; using Microsoft.SqlServer.Management.Smo; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { StreamWriter writer = new StreamWriter(@"c:\script_output.txt", false); Server s = new Server("servername"); Database db = s.Databases["AdventureWorks"]; // Set up the scripting options to be used below. ScriptingOptions so = new ScriptingOptions(); so.Permissions = true; so.Indexes = true; so.IncludeIfNotExists = true; // First script the database itself StringCollection coll = db.Script(so); foreach (string str in coll) { writer.WriteLine(str); } // Now script each of the tables foreach (Table t in db.Tables) { StringCollection col2 = t.Script(so); foreach (string str2 in col2) writer.WriteLine(str2); } // Now script each of the Users foreach (User u in db.Users) { StringCollection col3 = u.Script(so); foreach (string str3 in col3) writer.WriteLine(str3); } // Now Script each of the Userdefinedfunc foreach (UserDefinedFunction d in db.UserDefinedFunctions) { StringCollection col4 = d.Script(so); foreach (string str4 in col4) writer.WriteLine(str4); } Backup bkp = new Backup(); bkp.Devices.AddDevice(@"C:\AdventureWorks.bak", DeviceType.File); //bkp.Database(db); bkp.Database = "AdventureWorks"; bkp.Action = BackupActionType.Database; bkp.Initialize = true; bkp.PercentCompleteNotification = 10; bkp.PercentComplete += new PercentCompleteEventHandler(bkp_PercentComplete); bkp.SqlBackup(s); writer.Flush(); writer.Close(); } static void bkp_PercentComplete(object sender, PercentCompleteEventArgs e) { Console.WriteLine(e.Percent.ToString() + "% backed up"); } } }

Tonci on 9/15/2009
Just Excellent. Thank you very much.

Will Casey on 5/5/2011
cracking demo

Patrick Callahan on 5/5/2011
How about a copy of the source code shown?

erick on 5/5/2011
This was great! can we have another one to compare objects in a different server?

Carlos B. Vasquez on 5/5/2011
great job..

Raymond on 5/5/2011
Thanks

Max Turavani on 5/5/2011
nice.

Max Turavani on 5/5/2011
beauty

Don Weigend on 5/5/2011
What is a good source to start learning how to program in SQL Server SMO?

Oded on 5/5/2011
I try to run the code with VS 2010 and I got error on // Add SMO namespaces for less typing later using Microsoft.SqlServer.Management.Smo; Error 5 The type or namespace name 'Management' does not exist in the namespace 'Microsoft.SqlServer'

Buck Webb on 5/5/2011
Really good, to the point, and is the first time I've bothered to look at SMO. I feel like going after it big time now. Thanks for taking the time to push this information out!

Leonard Peoples on 5/5/2011
Very cool.

Keith Badeau on 5/5/2011
Great video. I decided to try my hand at SMO and found that I had to manually browse for and add references to the corresponding assemblies. Very clean and easy.

Ludwig on 5/5/2011
Execellent...Keep up the great work..it's a habbit of mine to wtahc these vidoes whenever I can ...Thx

Steve Duck on 5/5/2011
BAD AUDIO...PLEASE REPOST.

Russell Tye on 5/5/2011
Great demo...

s on 5/6/2011
it is very clear and explanatory

Anil Babu on 5/9/2011
Thanks.. very good

John Miceli on 5/9/2011
Very handy to know.

Russell Todd on 5/14/2011
vb.net please

Raman on 5/17/2011
Good to know the SMO..i am beginer

Raman on 5/17/2011
Good to know the SMO..i am beginer

Steve Harris on 6/9/2011
what are the other options of scrpting that allowed to to include indexes and such

Maurice Ivory on 10/18/2011
I will have to say that the task was made much easier by SMO. I can see the usefullness in this.



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions