简体   繁体   English

如何以编程方式检查SQL数据库/服务器是否支持C#压缩?

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

Building a tool to backup a database that I'm testing locally first. 构建一个工具来备份我首先要在本地测试的数据库。 I'm trying to check if compression is supported and change my SQL query based on the value returned. 我正在尝试检查是否支持压缩,并根据返回的值更改SQL查询。 Running it against an actual live server returns 0 or 1, but running the query itself against a local instance doesn't give a value. 在实际的实时服务器上运行它会返回0或1,但是在本地实例上运行查询本身不会得到任何值。 As a result I don't believe the if statements ever run to change the CommandText and my final WriteLine test returns the initial compression query instead of one of the backup commands. 结果,我不相信if语句会运行来更改CommandText而我的最终WriteLine测试将返回初始压缩查询,而不是备份命令之一。

I tried changing the if to check for a null, however the else should catch any other values besides '1' 我尝试更改if以检查是否为空,但是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();

It should return one of the nested backup commands. 它应该返回嵌套的备份命令之一。 Right now it simply writes the initial compression query. 现在,它只编写初始压缩查询。

Backup compression is not available in all editions of SQL Server. 备份压缩并非在所有版本的SQL Server中都可用。 So in my sqlexpress the value is not even in the table, it is probably your case too. 因此,在我的sqlexpress中,该值甚至不在表中,也可能是您的情况。 reader.Read() never reads anything, so you do not get into the if part at all. reader.Read()从不读取任何内容,因此您根本不会进入if部分。 You can restructuralize you code 您可以重组代码

bool canCompress = false;

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

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

And you can even simplify the reading like this 您甚至可以像这样简化阅读

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

The following code gets a value which indicates whether backup compression is supported and, if so, whether it is enabled by default. 以下代码获取一个值,该值指示是否支持备份压缩,如果支持,则默认情况下是否启用备份压缩。

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

Note that the value column in sys.configurations is declared as sql_variant . 请注意, sys.configurations中的value列声明为sql_variant The actual type returned can be displayed thusly: 返回的实际类型可以这样显示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中以编程方式创建SQL Server数据库 - How to create a SQL Server database programmatically in C# 使用C#以编程方式创建SQL Server数据库 - Create SQL Server database programmatically with C# 如何检查SQL Server数据库C#winform中是否存在记录? - How to check if a record exists in the SQL Server database C# winform? 如何在C#中检查与SQL Server数据库的成功连接? - How to check successful connection to a SQL Server database in C#? 如何以编程方式更改 C# 中的图片压缩? - How to change picture compression in C# programmatically? 如何在 C# 中以编程方式创建 SQL Server 数据库 - 为每个项目创建新 SQL Server 数据库的项目管理应用程序 - How to create SQL Server database programmatically in C# - Project management application that creates a new SQL Server database for each project 如何以编程方式对 C# SQL Server 数据库进行排序并将每个项目显示为单独的按钮? - How do I sort a C# SQL Server database programmatically and display each item as a separate button? 如何从C#以编程方式创建SQL Server数据库文件(.mdf)? - How can I create a SQL Server database file (.mdf) programmatically from C#? 如何以编程方式测试SQL Server是否支持“ AT TIME ZONE”? - How can I programmatically test if SQL Server supports 'AT TIME ZONE'? C#:如何以编程方式将SQL脚本导入数据库? - C#: How to import SQL-script into database programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM