gpt4 book ai didi

sql-server - 如何以编程方式克隆 SQL Server 中的数据库架构?

转载 作者:行者123 更新时间:2023-12-04 07:09:57 27 4
gpt4 key购买 nike

我想对我的项目进行一些可靠的测试,我想做的方式是拥有一个可以运行的命令行程序,该程序将仅复制数据库的结构而不是实际数据。然后我可以在那个新数据库上运行我的测试。

想法?

更新:有人说我应该指定一种语言。我认为 TSQL 就是这样,如果 Sql Server 运行,脚本应该运行。此外,它是 SQL Server 2005。

最佳答案

你不会说你使用的是哪个版本的 SQL Server,如果它的 2005 看看 DMO - 数据库管理对象,一组 COM 库,让你访问企业管理器/管理工作室中的功能。

2008 年我们有 SMO 类似的功能,但作为 .Net 程序集,MS 有一些 good code examples .他们的脚本示例看起来像你需要的:-

从他们的网站上解除了我们有

//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);
}
}
}
}

关于sql-server - 如何以编程方式克隆 SQL Server 中的数据库架构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/539067/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com