[英]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.