使用 SMO 时如何禁用评论

How to disable comment when using SMO

我正在编写一个 SMO 脚本来将我的 SQL Server 2016 数据库导出到一个文本文件。然而 Scripter 总是在我的脚本之前添加这条评论:

/*    ==Scripting Parameters==

    Source Server Version : Version140 (14.0.600)
    Source Database Engine Edition : Enterprise
    Source Database Engine Type : Standalone

    Target Server Version : Version140
    Target Database Engine Edition : Enterprise
    Target Database Engine Type : Standalone
*/

如何禁用此评论的生成?

我尝试将 IncludeHeaders 设置为 false/true 但这是 enables/disables 另一个评论。

我是这样使用 SMO 的:

var connectionString = "...";
var serverConnection = new ServerConnection(new SqlConnection(connectionString));
var sqlServer = new Smo.Server(serverConnection);
var createDbScriptOptions = new Smo.ScriptingOptions();
createDbScriptOptions.NoFileGroup = true;
createDbScriptOptions.IncludeFullTextCatalogRootPath = false;
StringCollection strcoll = db.Script(createDbScriptOptions);
foreach (String st in strcoll)
{
    Console.WriteLine(st);
}

或者在使用

编写表或其他对象脚本时
scripter.Script(new Urn[] { tb.Urn }

scripter.EnumScript(new Urn[] { tb.Urn }

评论也被添加。

你试过吗...

createDbScriptOptions.IncludeDatabaseContext = false;

https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.scriptingoptions.includedatabasecontext.aspx

显然这是 SSMS v17.2 附带的一项新功能,目前还没有用于关闭它的 SMO 脚本选项标志。应该在下一版本的 SSMS 中。

在 Microsoft Connect 上查看 post:

Posted by Microsoft on 8/29/2017 at 1:44 PM I'll be adding a new scripting option for producing the header in the next release of SMO/SSMS. This will be false for default which means you will not get the new header unless you specifically enable it. Thanks for the feedback! -Charles Gagnon (chgagnon@microsoft.com)

致电createDbScriptOptions.IncludeScriptingParameters = false;

https://docs.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.management.smo.scriptingoptions.includescriptingparametersheader?view=sql-smo-160