加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 08:52:39 所属栏目:MsSql教程 来源:网络整理
导读:在SSMS中,当我点击数据库“任务 – 生成脚本”时,我得到一个输出脚本. 如何使用非图形工具进行相同操作? 解决方法 图形工具只是实际实现脚本的SMO类的一个包装,就像 Scripter类.有一个示例使用MSDN中的SMO在数据库中编写所有表: Scripting: //Connect to t
在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); 
         } 
      } 
   } 
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读