2017-07-20 51 views
1

通過比較DacPac文件和目標數據庫,目前使用SqlPackage.exe命令行工具生成腳本。這將是Create the database if it doesn't already existSSDT如何從DACPAC文件創建新的數據庫?

我們現在想切換到使用Microsoft.Data.Tools.Msbuild Nuget包(使用C#部署),但是當數據庫容器不存在時,使用SchemaComparison進行比較將失敗。似乎沒有太多有關該軟件包的文檔。

作爲這個包的一部分,是否有任何工具可以讓我在比較之前創建一個空的數據庫容器?

我目前使用下面的代碼(對現有的數據庫進行比較時,其工作原理):

SchemaCompareDacpacEndpoint sourceDacpac = new SchemaCompareDacpacEndpoint(@"C:\DeployDB.dacpac"); 

     SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(); 
     csb.DataSource = @"."; 
     csb.InitialCatalog = "DeployTestDB"; 
     csb.IntegratedSecurity = true; 
     SchemaCompareDatabaseEndpoint targetDatabase = new SchemaCompareDatabaseEndpoint(csb.ToString()); 


     SchemaComparison comparison = new SchemaComparison(sourceDacpac, targetDatabase); 
     comparison.Options.DropObjectsNotInSource = true; 
     comparison.Options.BlockOnPossibleDataLoss = false; 
     comparison.Options.TreatVerificationErrorsAsWarnings = true; 
     comparison.Options.ScriptDatabaseOptions = true; 
     comparison.Options.GenerateSmartDefaults = true; 

     SchemaComparisonResult comparisonResult = comparison.Compare(); 

在這一點上,ComparisonResult有一條錯誤消息: {錯誤SQL0:無法打開數據庫由登錄請求的「DeployTestDB」。登錄失敗,用戶'myUsername'登錄失敗。}

+0

不正是你所要求的是什麼,但爲什麼不手動創建數據庫,如果它不存在?如果不存在(select * from sys.databases where name ='test')create database test; –

回答

0

查看Microsoft.SqlServer.Dac.dll中的DacServices類。

的代碼會是這個樣子:

using Microsoft.SqlServer.Dac; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     DacServices ds = new DacServices(@"Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=true"); 
     using (DacPackage dp = DacPackage.Load(@"C:\temp\mydb.dacpac")) 
     { 
      ds.Deploy(dp, @"DATABASENAME", upgradeExisting: false, options: null, cancellationToken: null); 
     } 
    } 
} 
相關問題