SQL Server 2008-使用命令行创建数据库脚本(模式+数据) [英] SQL Server 2008 - create database script (schema + data) with command-line

查看:253
本文介绍了SQL Server 2008-使用命令行创建数据库脚本(模式+数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SSMS中,当我单击数据库任务->生成脚本时,得到一个输出脚本。

In SSMS when I click on the database "Tasks -> Generate Scripts", I get an output script.

如何使用非图形工具执行同样的操作?

How do I do the same with a non-graphical tool?

推荐答案

图形工具只是实际实现脚本的SMO类的包装,例如 Scripter 类。在MSDN中,有一个使用SMO编写数据库中所有表脚本的示例:脚本编写

The graphical tool is just a wrapper around the SMO classes that actually implement scripting, like the Scripter class. There is an example of scripting all tables in a database with SMO in MSDN: 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); 
         } 
      } 
   } 
} 

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

There are many more examples how to use it on various other sites.

这篇关于SQL Server 2008-使用命令行创建数据库脚本(模式+数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆