This post is showing you how to generate the scripts for all the database structure in a SQL Server.
You can right click the database in SQL Server Management Studio for sure but in that way you can only generate the scripts for one database at a time.
Using the code below you are able to generate all the database at one time.
public
static
void GenerateDBScript( string strSQLServer, Server server)
{
StreamWriter sw = new StreamWriter(strSQLServer + “.sql” );
Scripter scripter = new Scripter(server);
Database myAdventureWorks = server.Databases[strSQLServer];
Urn[] DatabaseURNs = new Urn[] { myAdventureWorks.Urn };
StringCollection scriptCollection = scripter.Script(DatabaseURNs);
ScriptingOptions scriptOptions = new ScriptingOptions();
scriptOptions.ScriptDrops = true ;
scriptOptions.IncludeIfNotExists = true ;
foreach (Table myTable in myAdventureWorks.Tables)
{
StringCollection tableScripts = myTable.Script(scriptOptions);
foreach ( string script in tableScripts)
sw.WriteLine(script);
tableScripts = myTable.Script();
foreach ( string script in tableScripts)
sw.WriteLine(script);}
sw.Close();
}
To call the code above using the code in the main method below.
static
void Main( string [] args)
{
string strServer = “172.18.123.74” ;
string strUid = “sa” ;
string strPwd = “password” ;
string strDb = “Dashboard15” ;
string strconn = “Data Source=” + strServer +
“;Initial Catalog= “ + strDb +
“;User ID=” + strUid +
“;Password=” + strPwd;
SQLUtils.CreateeDB(strconn, “c:\\database” , “ExpIncPm” );
}
You can right click the database in SQL Server Management Studio for sure but in that way you can only generate the scripts for one database at a time.
Using the code below you are able to generate all the database at one time.
public
static
void GenerateDBScript( string strSQLServer, Server server)
{
StreamWriter sw = new StreamWriter(strSQLServer + “.sql” );
Scripter scripter = new Scripter(server);
Database myAdventureWorks = server.Databases[strSQLServer];
Urn[] DatabaseURNs = new Urn[] { myAdventureWorks.Urn };
StringCollection scriptCollection = scripter.Script(DatabaseURNs);
ScriptingOptions scriptOptions = new ScriptingOptions();
scriptOptions.ScriptDrops = true ;
scriptOptions.IncludeIfNotExists = true ;
foreach (Table myTable in myAdventureWorks.Tables)
{
StringCollection tableScripts = myTable.Script(scriptOptions);
foreach ( string script in tableScripts)
sw.WriteLine(script);
tableScripts = myTable.Script();
foreach ( string script in tableScripts)
sw.WriteLine(script);}
sw.Close();
}
To call the code above using the code in the main method below.
static
void Main( string [] args)
{
string strServer = “172.18.123.74” ;
string strUid = “sa” ;
string strPwd = “password” ;
string strDb = “Dashboard15” ;
string strconn = “Data Source=” + strServer +
“;Initial Catalog= “ + strDb +
“;User ID=” + strUid +
“;Password=” + strPwd;
SQLUtils.CreateeDB(strconn, “c:\\database” , “ExpIncPm” );
}
The code above is located at https://github.com/chanmmn/SQLUtilities/tree/master/ConAppGenerateScriptsForAllDBs .