2014-10-31 156 views
0

運行下面的代碼時,爲什麼會出現此錯誤?這經常發生。我有到位的開放連接的支票......連接未關閉錯誤

System.InvalidOperationException: The connection was not closed. 
    at System.Data.EntityClient.EntityConnection.Open() 
    at Publish(Int32 reportID) 

CODE:

while (!completed && 5 >= retryCount) 
{ 
    retryCount++; 
    try 
    { 
     internalReportsEntities.CommandTimeout = (5 * 60); 
     internalReportsEntities.Publish(internalSet.ID); // publish method called 
     completed = true; 
    } 
    catch (EntityCommandExecutionException entityCommandExecutionException) 
    { 
     if (5 < retryCount) throw; 
     Utilities.ThreadUtilities.Sleep(retryCount); 
    } 
} 

public void Publish(int reportID) 
{ 
if (this.Connection.State != System.Data.ConnectionState.Open) this.Connection.Open(); 
EntityCommand publishEntityCommand = new EntityCommand 
    { 
      CommandType = CommandType.StoredProcedure, 
      CommandText = @"ReportsEntities.Publish", 
      Connection = (EntityConnection)this.Connection 
    }; 
} 
+0

如果狀態是壞掉了怎麼辦?然後你需要關閉並重新打開。 – Reniuz 2014-10-31 10:45:13

回答