Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hopefully a it would guide you and upcoming ones.</p> <p>You have to add following four references to your project to include following required namespaces </p> <p>To add a references</p> <ol> <li>Right click your project in solution explorer and choose add reference</li> <li>Click Browse from upper menu</li> <li>And choose 4 dll files as instructed below</li> </ol> <p><a href="https://stackoverflow.com/questions/6453415/reference-microsoft-sqlserver-smo-dll">Reference Microsoft.SqlServer.Smo.dll</a></p> <p>namespaces</p> <pre><code>using System.Data.SqlClient; using System.Collections.Specialized; using Microsoft.SqlServer.Management.Smo; </code></pre> <p>Now use following code in any function or button click event</p> <pre><code> // For Me Server is ".\SQLExpress" You may have changed Server myServer = new Server(@".\SQLExpress"); Scripter scripter = new Scripter(myServer); //Databas1 is your database Name Thats Changable Database myAdventureWorks = myServer.Databases["Database1"]; /* With ScriptingOptions you can specify different scripting * options, for example to include IF NOT EXISTS, DROP * statements, output location etc*/ ScriptingOptions scriptOptions = new ScriptingOptions(); scriptOptions.ScriptDrops = true; scriptOptions.IncludeIfNotExists = true; string scrs = ""; string tbScr = ""; foreach (Table myTable in myAdventureWorks.Tables) { /* Generating IF EXISTS and DROP command for tables */ StringCollection tableScripts = myTable.Script(scriptOptions); foreach (string script in tableScripts) scrs += script; /* Generating CREATE TABLE command */ tableScripts = myTable.Script(); foreach (string script in tableScripts) tbScr += script; } // For WinForms MessageBox.Show(scrs + "\n\n" + tbScr); //For Console //Console.WriteLine(scrs + "\n\n" + tbScr); </code></pre> <p>It involved <a href="http://www.mssqltips.com/sqlservertip/1833/generate-scripts-for-database-objects-with-smo-for-sql-server/" rel="nofollow noreferrer">http://www.mssqltips.com/sqlservertip/1833/generate-scripts-for-database-objects-with-smo-for-sql-server/</a> Answer (above) by David Brabant and the SO link above</p> <p>Code Block 2 is used. Now you can use others as well</p> <p>i could not find myserver there but it is resolved as well in above code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload