Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically generate SQL script to create database without PRIMARY statement .. using scripter

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 
like image 948
Tulsi Avatar asked May 05 '26 15:05

Tulsi


1 Answers

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);
}
like image 110
nathan_jr Avatar answered May 08 '26 05:05

nathan_jr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!