System.Data;超时已过期。从池中获取连接之前经过的超时时间。这可能是因为所有池连接都在使用中并且达到了最大池大小。 只有当我运行服务
时才会出现此错误我正在使用以下代码
try
{
LogGenerator.WriteErrorLog("Conn opened before");
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
LogGenerator.WriteErrorLog("Conn opened");
// Set up a command with the given query and associate
// this with the current connection.
SqlCommand cmd = new SqlCommand("Backup database " + DBName + " to disk='" + filePathExist + "'", con);
cmd.ExecuteNonQuery();
LogGenerator.WriteErrorLog("query executed");
}
catch(Exception ex)
{
LogGenerator.WriteErrorLog("Error in Conn");
LogGenerator.WriteErrorLog(ex);
}
finally
{
con.Close();
con.Dispose();
SqlConnection.ClearPool(con);
}
答案 0 :(得分:0)
当您尝试进行数据库备份时,它将需要超过默认连接时间,因此请尝试将超时设置为您的命令。
try
{
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
// Set up a command with the given query and associate
// this with the current connection.
SqlCommand cmd = new SqlCommand("Backup database " + DBName + " to disk='" +
filePathExist + "'", con);
cmd.CommandTimeout = 60;
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
//handle exception here
}
finally
{
con.Close();
con.Dispose();
SqlConnection.ClearPool(con);
}
有关超时的更多信息,请参阅此处。 https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout(v=vs.110).aspx