当我双击此文件并发布时,我们有SQLServer01.Publish.xml
,它将数据库发布到sqlServer01。
我想问一下,我们能否以某种方式从代码中发布此个人资料?
答案 0 :(得分:5)
SSDT似乎喜欢DacPac这种事情。 DacServices
中有一个Microsoft.SqlServer.Dac
实用程序类。我认为这将要求在计划运行此代码的计算机上安装SSDT。
public class DacPacUtility
{
public void DeployDacPac( string connString, string dacpacPath, string targetDbName )
{
var dbServices = new DacServices( connString );
var dbPackage = DacPackage.Load( new FileStream( dacpacPath, FileMode.Open, FileAccess.Read ), DacSchemaModelStorageType.Memory, FileAccess.Read );
var dbDeployOptions = new DacDeployOptions()
{
SqlCommandVariableValues =
{
new KeyValuePair< string, string >( "debug", "false" )
},
CreateNewDatabase = true,
BlockOnPossibleDataLoss = false,
BlockWhenDriftDetected = false
};
dbServices.Deploy( dbPackage, targetDbName, upgradeExisting : true, options : dbDeployOptions );
}
}
奖励:您可以使用Microsoft.Build.Evaluation.Project
命名空间来新建Project
对象,并使用生成的内置dacpac在本地构建它以进行集成测试,以初始化测试。< / p>
[SetUpFixture]
public class TestSetup
{
[SetUp]
public void SetUpTests()
{
var projectPath = @"C:SomeDirectory";
var project = new Project( projectPath );
project.Build();
ProjectCollection.GlobalProjectCollection.UnloadProject( project );
var dacPac = new DacPacUtility();
var connString = "Data Source=(localdb)\ProjectsV12;Initial Catalog=Tests;Integrated Security=True";
var dacPacPath = projectPath + "..\bin\projectName.dacpac";
dacPac.DeployDacPac(connString, dacPacPath, "Tests");
}
[TearDown]
public void TearDownTests()
{
// TODO: delete db or run other cleanup scripts
}
}
<强>参考强>:
Deborah's Developer MindScape: Deploying A DACPAC
从何处获取Microsoft.SqlServer.Dac
以及DacService
有两种方法可以获取Microsoft.SqlServer.Dac
库: