本文介绍了生成数据库创建脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能产生从.NET SQL Server数据库?

Is it possible to generate the database creation scripts for a SQL server database from .NET?

我使用C#和我想建立某种形式的一个安装项目我的应用程序关于这一点我可以选择一个现有的数据库,生成创建脚本,并在另一个SQL Server实例上运行它们。

I am using C# and I would like to create some sort of an installer project for my applicationon which I can select an existing database, generate the creation scripts and run them on another SQL server instance.

推荐答案

是的,这是可能的。这很容易与 SMO 做到这一点,看到的类脚本操作和的类(创建,删除等)。用法是这样的:

Yes, it is possible.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();
    }

这篇关于生成数据库创建脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:29
查看更多