Generate SQL script programmatically to create database without PRIMARY .. statement using script

I created a script that is automatically generated by the method Scripter.Script()

. But if you want to get rid of the file paths included in the script. What property needs to be set when using the Scripter class to generate a script?

SQL Script:

CREATE DATABASE [ContactManager] ON  PRIMARY 
( NAME = N'ContactManager', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\ContactManager.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'ContactManager_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\ContactManager.ldf' , SIZE = 5184KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

      

but now I want to generate a script without main statement like below:

CREATE DATABASE [ContactManager] GO 

      

+3


source to share


1 answer


ScriptingOptions.NoFileGroup

should suppress additional filegroup clauses.

More information here .

Also, it seems like just calling db.Script()

without an included property will skip the filegroup anyway, so I'm not sure if this will work for you without seeing your code.



See my example below:

Microsoft.SqlServer.Management.Smo.Server srv = new Server(".");
Microsoft.SqlServer.Management.Smo.Database db = new Database(srv, "Yak");       

System.Collections.Specialized.StringCollection statements = db.Script();

Console.WriteLine(statements);

ScriptingOptions so = new ScriptingOptions();
so.NoFileGroup = false;


foreach (string s in db.Script(so))
{
      Console.WriteLine(s);
}

      

+4


source







All Articles