3

I have create script which is automatically generated by Scripter.Script() method. But if want to get rid of file paths included in the script. Which property is need to be set while using Scripter class to generate 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 script without primary statement like below:

CREATE DATABASE [ContactManager] GO 
Tulsi
  • 151
  • 3
  • 15
  • `CREATE DATABASE ContactManager;` is a valid statement. The primary statement is optional. If you dont specify the file names SQL will just use the database name. – nathan_jr Jan 25 '13 at 06:00
  • @NathanSkerl kindly refer the edited question – Tulsi Jan 25 '13 at 07:08

1 Answers1

4

ScriptingOptions.NoFileGroup should suppress the additional filegroup clauses.

More info here.

Also, it seems that simply calling db.Script() without the property enabled omits the filegroup anyways, so I am not positive 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);
}
nathan_jr
  • 9,092
  • 3
  • 40
  • 55