【发布时间】:2017-05-16 12:15:12
【问题描述】:
我的应用程序用于自动恢复数据库。
我创建了一个获取还原完成百分比的函数。
问题是它执行得越多,它就越慢。我的电脑性能变得非常糟糕,一切都很慢。
我认为请求中有问题。
private void GetPercentRestore(string dbName, string query)
{
// T-SQL command to have information on the backup
query = query.Replace("'", "''");
string cmd =
$"SELECT sysdb.NAME AS [Name], dmv.PERCENT_COMPLETE AS [PercentComplete] FROM sys.databases sysdb INNER JOIN sys.dm_exec_requests dmv ON sysdb.database_id = dmv.database_id CROSS APPLY sys.dm_exec_sql_text(dmv.SQL_HANDLE) dest WHERE TEXT LIKE '{query}';";
int percent = 0;
int lastPercent = -1;
// Initialize the new connection : need because two query at the same time
SqlConnection conn =
new SqlConnection(
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionCluster"].ToString());
SqlCommand command = new SqlCommand(cmd, conn);
command.CommandTimeout = 3600;
try
{
while (percent < 100)
{
conn.Open();
// Execute the command
var result = command.ExecuteReader();
// Count the total of the size of the backup
if (result.HasRows)
{
result.Read();
//ProgressBarViewModel e = new ProgressBarViewModel();
percent = Convert.ToInt32(result["PercentComplete"]);
}
else // if not result, assume that Restore is complete
percent = 100;
if (percent != 100)
Thread.Sleep(10000);
result.Close();
conn.Close();
}
}
catch (Exception e)
{
LogFile.WriteError(e.Message);
}
conn.Close();
}
【问题讨论】:
-
所以您正在执行还原并尝试同时从数据库查询?这可能是普遍的问题。否则我建议在
Sleep之前关闭连接,看看是否有帮助。也许也可以将连接和命令放在using语句中。 -
juharr 是对的。遍历所有 ADO.NET 对象,如果它实现了 IDispose,请在它们周围放置 using 块。
标签: c# mysql sql .net database