我想在SSMS 2008中自动生成脚本(在SSMS->任务->生成脚本中)。我已经读到SQL Server 2008不支持数据库发布向导(包括SQLPUBWIZ SCRIPT),但是可以使用SMO来完成此自动化操作。在SQL Server 2008中。关于SMO以及如何使用SMO做到这一点,我一无所知,因此,您能给我一些建议(资源等)如何开始吗?
最佳答案
SMO脚本编写的关键是 Scripter
类。所有其他工具(如SSMS)都使用此类来生成对象创建脚本。 MSDN上有一个用法示例:
{
//Connect to the local, default instance of SQL Server.
Server srv = new Server();
//Reference the AdventureWorks2008R2 database.
Database db = srv.Databases["AdventureWorks2008R2"];
//Define a Scripter object and set the required scripting options.
Scripter scrp = new Scripter(srv);
scrp.Options.ScriptDrops = false;
scrp.Options.WithDependencies = true;
//Iterate through the tables in database and script each one. Display the script.
//Note that the StringCollection type needs the System.Collections.Specialized namespace to be included.
Microsoft.SqlServer.Management.Sdk.Sfc.Urn[] smoObjects = new Microsoft.SqlServer.Management.Sdk.Sfc.Urn[1] ;
foreach (Table tb in db.Tables) {
smoObjects[0] = tb.Urn;
if (tb.IsSystemObject == false) {
System.Collections.Specialized.StringCollection sc;
sc = scrp.Script(smoObjects);
foreach ( string st in sc) {
Console.WriteLine(st);
}
}
}
}
关于sql-server - 如何在SQL Server中使用SMO自动生成脚本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3488666/