微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql-server – SQL Server 2008 – 使用命令行创建数据库脚本(模式数据)

在SSMS中,当我点击数据库“任务 – >生成脚本”时,我得到一个输出脚本.

如何使用非图形工具进行相同操作?

解决方法

图形工具只是实际实现脚本的SMO类的一个包装,就像 Scripter类.有一个示例使用MSDN中的SMO在数据库中编写所有表: Scripting
//Connect to the local,default instance of sql Server. 
{ 
   Server srv = default(Server); 
   srv = new Server(); 
   //Reference the AdventureWorks database. 
   Database db = default(Database); 
   db = srv.Databases("AdventureWorks"); 
   //Define a Scripter object and set the required scripting options. 
   Scripter scrp = default(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. 
   Table tb = default(Table); 
   Urn[] smoObjects = new Urn[2]; 
   foreach ( tb in db.Tables) { 
      smoObjects = new Urn[1]; 
      smoObjects(0) = tb.Urn; 
      if (tb.IsSystemObject == false) { 
         StringCollection sc = default(StringCollection); 
         sc = scrp.Script(smoObjects); 
         string st = null; 
         foreach ( st in sc) { 
            Console.WriteLine(st); 
         } 
      } 
   } 
}

还有更多的例子如何在各种其他网站上使用它.

原文地址:https://www.jb51.cc/mssql/75072.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐