如何以编程方式检查 SQL database/server 是否支持 C# 中的压缩?

How to programmatically check if a SQL database/server supports compression in C#?

构建一个工具来备份我首先在本地测试的数据库。我正在尝试检查是否支持压缩并根据值 returned 更改我的 SQL 查询。 运行 它针对实际的实时服务器 returns 0 或 1,但是 运行 针对本地实例的查询本身没有给出值。因此,我不相信 if 语句曾经 运行 改变 CommandText 和我最后的 WriteLine 测试 returns 初始压缩查询而不是备份命令之一.

我尝试更改 if 以检查是否为 null,但是 else 应该捕获除“1”之外的任何其他值

string compressionQuery = "SELECT VALUE FROM sys.configurations WHERE name = 'backup compression default'";
SqlCommand sqlCmd = new SqlCommand(compressionQuery, newConn);

SqlDataReader reader = sqlCmd.ExecuteReader();

while (reader.Read()) //while the data reader is checking the records
{
    Interface.WriteLine(reader.GetInt32(0).ToString()); //print the specified record(row) to the console
    canCompress = reader.GetInt32(0);

    // Backup the database.
    if (canCompress == 1)
    {
        sqlCmd.CommandText = "BACKUP DATABASE [" + connBuilder.InitialCatalog + "] "
            + "TO DISK = '" + backupPath + "' "
            + "WITH COPY_ONLY, COMPRESSION, NOFORMAT, NOINIT, "
            + "NAME = '" + backupName + "', "
            + "SKIP, REWIND, NOUNLOAD, STATS = 10";
        Interface.WriteLine("1");
    }
    else 
    {
        sqlCmd.CommandText = "BACKUP DATABASE [" + connBuilder.InitialCatalog + "] "
            + "TO DISK = '" + backupPath + "' "
            + "WITH COPY_ONLY, NOFORMAT, NOINIT, "
            + "NAME = '" + backupName + "', "
            + "SKIP, REWIND, NOUNLOAD, STATS = 10";
        Interface.WriteLine("0");
    }
}

reader.Close(); //stop reading records

Interface.WriteLine(sqlCmd.CommandText.ToString()); //Should print one of the backup queries
sqlCmd.ExecuteNonQuery();

它应该return 嵌套备份命令之一。现在它只写初始压缩查询。

并非所有版本的 SQL Server 都提供备份压缩。因此,在我的 sqlexpress 中,该值甚至不在 table 中,这可能也是您的情况。 reader.Read() 从不读取任何内容,因此您根本不会进入 if 部分。你可以重构你的代码

bool canCompress = false;

using (SqlDataReader reader = sqlCmd.ExecuteReader())
{
    while (reader.Read())
    {
        canCompress = reader.GetInt32(0) == 1;                                      
    }
}

if (canCompress)
{
    ...
}
else
{
    ...
}

你甚至可以像这样简化阅读

bool canCompress = (int?)sqlCmd.ExecuteScalar() == 1;

以下代码获取一个值,表示是否支持备份压缩,如果支持,是否默认启用。

using (SqlConnection dbConnection = new SqlConnection("Your connection string.")
{
    dbConnection.Open();
    using (SqlCommand dbCommand = new SqlCommand(
      "select value from sys.configurations where name = 'backup compression default';", dbConnection))
    {
        // The values are:
        //   null  Backup compression is not supported.
        //   0     Backup compression is supported and disabled by default.
        //   1     Backup compression is supported and enabled by default.
        int? backupCompressionDefault = (int?)dbCommand.ExecuteScalar();
    }
    dbConnection.Close();
}

请注意,sys.configurations 中的 value 列声明为 sql_variant。实际返回的类型可以这样显示:

select SQL_VARIANT_PROPERTY( value, 'basetype' )
  from sys.configurations
  where name = 'backup compression default';