How do I find the current version of a SQL Server data-tier application?

前端 未结 2 727
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 10:06

We are using a SQL Server Data-tier application (dacpac or DAC pack) and I\'m having a hard time finding the current version of the database.

Is there a way to obtai

2条回答
  •  有刺的猬
    2020-12-31 10:18

    In DacFx 3.0 the DacStore is no longer available. To get the version from C# code you need to query the database. Here's an example:

      using System;
      using System.Data;
      using System.Data.SqlClient;
    
      class Program
      {
        static void Main()
        {
          try
          {
            string version = GetDatabaseVersion(@"Initial Catalog=xxx;Data Source=yyy;Integrated Security=True;Pooling=False", false);
            Console.WriteLine("Database has DAC pack version {0}.", version);
          }
          catch (Exception ex)
          {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex.Message);
            Console.WriteLine();
            Console.ResetColor();
          }
          Console.WriteLine("Press any key to exit");
          Console.ReadKey(true);
        }
    
    /// 
    /// Gets the database version.
    /// 
    /// The connection string of database to query.
    /// True if we are querying an Azure database.
    /// DAC pack version
    private static string GetDatabaseVersion(string connectionString, bool isAzure)
    {
      var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
      string instanceName = connectionStringBuilder.InitialCatalog;
      string databaseToQuery = "msdb";
      if (isAzure)
      {
        // On Azure we must be connected to the master database to query sysdac_instances
        connectionStringBuilder.InitialCatalog = "Master";
        databaseToQuery = "master";
      }
    
      string query = String.Format("select type_version from {0}.dbo.sysdac_instances WHERE instance_name = '{1}'", databaseToQuery, instanceName);
      using (var connection = new SqlConnection(connectionStringBuilder.ConnectionString))
      {
        connection.Open();
        SqlCommand command = connection.CreateCommand();
        command.CommandText = query;
        command.CommandTimeout = 15;
        command.CommandType = CommandType.Text;
        var version = (string)command.ExecuteScalar();
        return version;
      }
    }
    

    }

提交回复
热议问题