是否有可能产生从.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 application on 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();
}