如何查找 SQL Server 数据层应用程序的当前版本?

发布于 2024-09-11 12:26:42 字数 194 浏览 5 评论 0原文

我们正在使用 SQL Server 数据层应用程序(dacpac 或 DAC 包),但我很难找到数据库的当前版本。

有没有办法使用以下任一方法获取当前版本:

  1. 从 SQL Server Management Studio 内部
  2. 通过 SQL 语句
  3. 使用 .NET 代码以编程方式获取

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 obtain the current version using any of these methods:

  1. From within SQL Server Management Studio
  2. Via a SQL statement
  3. Programmatically using .NET code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2024-09-18 12:26:42

从 SQL Server Management Studio 中

来自 http://msdn.microsoft.com /en-us/library/ee210574.aspx

要查看部署到数据库引擎实例的 DAC 的详细信息:

  1. 选择视图/对象资源管理器菜单。

  2. 对象资源管理器窗格连接到 的实例。

  3. 选择查看/对象资源管理器详细信息菜单。

  4. 对象资源管理器中选择映射到实例的服务器节点,然后导航到管理\数据层应用程序节点。

  5. 详细信息页面顶部窗格中的列表视图列出了部署到数据库引擎实例的每个 DAC。选择一个 DAC 以在页面底部的详细信息窗格中显示信息。

数据层应用程序节点的右键菜单还用于部署新的 DAC 或删除现有的 DAC。

通过 SQL 语句

SELECT instance_name, type_version FROM msdb.dbo.sysdac_instances

通过 Azure 上的 SQL 语句

SELECT instance_name, type_version FROM master.dbo.sysdac_instances

使用 .NET 代码以编程方式

请注意,在 DacFx 3.0 中这不再有效。请参阅我的其他答案以获取实现方法。

C#

ServerConnection serverConnection;
string databaseName;

// Establish a connection to the SQL Server instance.
using (SqlConnection sqlConnection =
    new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
    serverConnection = new ServerConnection(sqlConnection);
    serverConnection.Connect();

    // Assumes default database in connection string is the database we are trying to query.
    databaseName = sqlConnection.Database;
}

// Get the DAC info.
DacStore dacstore = new DacStore(serverConnection);
var dacInstance = dacstore.DacInstances[databaseName];
System.Diagnostics.Debug.Print("Database {0} has Dac pack version {1}.", databaseName, dacInstance.Type.Version);

VB.NET

Dim serverConnection As ServerConnection
Dim databaseName As String

' Establish a connection to the SQL Server instance.
Using sqlConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString)
    serverConnection = New ServerConnection(sqlConnection)
    serverConnection.Connect()

    ' Assumes default database in connection string is the database we are trying to query.
    databaseName = sqlConnection.Database
End Using

' Get the DAC info.
Dim dacstore As New DacStore(serverConnection)
Dim dacInstance = dacstore.DacInstances(databaseName)
System.Diagnostics.Debug.Print("Database {0} has Dac pack version {1}.", databaseName, dacInstance.Type.Version)

From within SQL Server Management Studio

From http://msdn.microsoft.com/en-us/library/ee210574.aspx

To view the details of a DAC deployed to an instance of the Database Engine:

  1. Select the View/Object Explorer menu.

  2. Connect to the instance of the from the Object Explorer pane.

  3. Select the View/Object Explorer Details menu.

  4. Select the server node in Object Explorer that maps to the instance, and then navigate to the Management\Data-tier Applications node.

  5. The list view in the top pane of the details page lists each DAC deployed to the instance of the Database Engine. Select a DAC to display the information in the detail pane at the bottom of the page.

The right-click menu of the Data-tier Applications node is also used to deploy a new DAC or delete an existing DAC.

Via a SQL statement

SELECT instance_name, type_version FROM msdb.dbo.sysdac_instances

Via a SQL statement on Azure

SELECT instance_name, type_version FROM master.dbo.sysdac_instances

Programmatically using .NET code

Note that in DacFx 3.0 this is no longer valid. See my other answer for a way to do it.

C#

ServerConnection serverConnection;
string databaseName;

// Establish a connection to the SQL Server instance.
using (SqlConnection sqlConnection =
    new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
    serverConnection = new ServerConnection(sqlConnection);
    serverConnection.Connect();

    // Assumes default database in connection string is the database we are trying to query.
    databaseName = sqlConnection.Database;
}

// Get the DAC info.
DacStore dacstore = new DacStore(serverConnection);
var dacInstance = dacstore.DacInstances[databaseName];
System.Diagnostics.Debug.Print("Database {0} has Dac pack version {1}.", databaseName, dacInstance.Type.Version);

VB.NET

Dim serverConnection As ServerConnection
Dim databaseName As String

' Establish a connection to the SQL Server instance.
Using sqlConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString)
    serverConnection = New ServerConnection(sqlConnection)
    serverConnection.Connect()

    ' Assumes default database in connection string is the database we are trying to query.
    databaseName = sqlConnection.Database
End Using

' Get the DAC info.
Dim dacstore As New DacStore(serverConnection)
Dim dacInstance = dacstore.DacInstances(databaseName)
System.Diagnostics.Debug.Print("Database {0} has Dac pack version {1}.", databaseName, dacInstance.Type.Version)
梦在夏天 2024-09-18 12:26:42

在 DacFx 3.0 中,DacStore 不再可用。要从 C# 代码获取版本,您需要查询数据库。这是一个例子:

  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);
    }

/// <summary>
/// Gets the database version.
/// </summary>
/// <param name="connectionString">The connection string of database to query.</param>
/// <param name="isAzure">True if we are querying an Azure database.</param>
/// <returns>DAC pack version</returns>
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;
  }
}

}

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);
    }

/// <summary>
/// Gets the database version.
/// </summary>
/// <param name="connectionString">The connection string of database to query.</param>
/// <param name="isAzure">True if we are querying an Azure database.</param>
/// <returns>DAC pack version</returns>
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;
  }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文