2014-05-09 39 views
-3

這裏我使用我的webmethod.But當我要連接它顯示此錯誤。但我的代碼是我認爲很好。ExecuteReader需要一個開放且可用的Connection。連接的當前狀態正在連接。在執行讀者

ExecuteReader需要一個開放且可用的Connection。連接的當前狀態正在連接。

mycode的

public static List<CommonPages> GetCommonPagesDescription(int Type) 
{ 
    List<CommonPages> CommonPageDescription = new List<CommonPages>(); 
    try 
    { 
     SqlCommand comGetAllFiles = new SqlCommand("GetCommonPageDescriptions", conDB); 
     comGetAllFiles.CommandType = CommandType.StoredProcedure; 
     if (conDB.State == ConnectionState.Closed) 
      conDB.Open(); // <-- Debugger Skip this & goto next line 
     comGetAllFiles.Parameters.Add("@Type", SqlDbType.Int); 
     comGetAllFiles.Parameters["@Type"].Value = Type; 

     SqlDataReader rdr = comGetAllFiles.ExecuteReader();//<-- error generating here 
     DataTable dt = new DataTable(); 
     dt.Load(rdr); 
     foreach (DataRow r in dt.Rows) 
     { 
      CommonPageDescription.Add(new CommonPages 
      { 
       Id = (int)r["Id"], 
       Description = r["Description"].ToString(), 
       Type = (int)r["Type"], 
       UpdatedDate = (DateTime)r["UpdatedDate"], 
       UpdatedBy = (Guid)r["UpdatedBy"] 

      }); 
     } 

    } 
    catch (Exception ee) 
    { 
    } 
    finally 
    { 
     conDB.Close(); 
    } 
    return CommonPageDescription; 
} 

conDB初始化這裏

static SqlConnection conDB = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString); 
+0

顯示其中conDB初始化代碼... – apomene

+1

這是很容易避免,如果你只需要創建一個新的連接對象,打開它,做你的工作,那麼,連接處置。順便說一句,空捕獲很少是件好事。 – LarsTech

+0

什麼是'connDB.State'? – usr

回答

1

而不是在關閉時打開連接,嘗試打開未打開時的連接。

public static List<CommonPages> GetCommonPagesDescription(int Type) 
{ 
    List<CommonPages> CommonPageDescription = new List<CommonPages>(); 
    try 
    { 
     SqlCommand comGetAllFiles = new SqlCommand("GetCommonPageDescriptions", conDB); 
     comGetAllFiles.CommandType = CommandType.StoredProcedure; 
     if (conDB.State != ConnectionState.Open) 
      conDB.Open(); // <-- Debugger Skip this & goto next line 
     comGetAllFiles.Parameters.Add("@Type", SqlDbType.Int); 
     comGetAllFiles.Parameters["@Type"].Value = Type; 

     SqlDataReader rdr = comGetAllFiles.ExecuteReader();//<-- error generating here 
     DataTable dt = new DataTable(); 
     dt.Load(rdr); 
     foreach (DataRow r in dt.Rows) 
     { 
      CommonPageDescription.Add(new CommonPages 
      { 
       Id = (int)r["Id"], 
       Description = r["Description"].ToString(), 
       Type = (int)r["Type"], 
       UpdatedDate = (DateTime)r["UpdatedDate"], 
       UpdatedBy = (Guid)r["UpdatedBy"] 

      }); 
     } 

    } 
    catch (Exception ee) 
    { 
    } 
    finally 
    { 
     conDB.Close(); 
    } 
    return CommonPageDescription; 
} 
1

conDB必須共享連接?可能不是一個好主意,可以利用連接池。

要解決該問題,請考慮按要求打開和關閉connection。不關心voodoo是否說這種效率低下,實際上你想盡可能少地打開/關閉連接,但有時你需要去數據庫出於單一的原因。有很多方法可以更好地分享連接,例如使用context模式。但是要解決你的即時問題結構,你的數據庫會這樣調用。

try 
{ 
    using(System.Data.Common.DbConnection conn = CreateConnection()) 
    { 
    //create your command... 
    //create your reader/or execute your command... 
    } 
} 
+0

如何解決它..請檢查我更新的問題 – TechGuy

相關問題