2014-05-09 38 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

相关问题