如何动态创建表脚本和存储过程脚本 [英] How to create Table scripts and Stored procedure scripts dynamically

查看:80
本文介绍了如何动态创建表脚本和存储过程脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要从C#或存储过程动态生成表脚本和存储过程脚本。是否可能?



例如,我们使用编写存储过程来生成下面链接中给出的插入脚本

在SQL Server中生成INSERT语句 [ ^ ]。我想对表脚本和存储过程脚本做同样的事情。





请告诉我任何可用的解决方案。 />


谢谢&问候,

Mathi。

Hi,

I need to generate Table scripts and Stored procedure scripts dynamically from either C# or through stored procedure. Is it possible ?

For Example, We use to write stored proc to generate insert scripts given in the below link
Generating INSERT statements in SQL Server[^]. I want to do the same for tables scripts and stored procs scripts.


Please let me know any solution available for this.

Thanks & Regards,
Mathi.

推荐答案



SMO ,请参阅传送;用于脚本操作的类和用于数据库操作的数据库类(创建,删除等)。用法如下所示:



Hi,
It's easy to do this with SMO, see Transfer; class for scripting operations and Database class for database operations (create, drop, etc). Usage looks like this:

private StringCollection GetTransferScript(Database database)
   {
       var transfer = new Transfer(database);

       transfer.CopyAllObjects = true;
       transfer.CopyAllSynonyms = true;
       transfer.CopyData = false;

       // additional options
       transfer.Options.WithDependencies = true;
       transfer.Options.DriAll = true;
       transfer.Options.Triggers = true;
       transfer.Options.Indexes = true;
       transfer.Options.SchemaQualifyForeignKeysReferences = true;
       transfer.Options.ExtendedProperties = true;
       transfer.Options.IncludeDatabaseRoleMemberships = true;
       transfer.Options.Permissions = true;
       transfer.PreserveDbo = true;

       // generates script
       return transfer.ScriptTransfer();


这篇关于如何动态创建表脚本和存储过程脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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