我的构思是这样的:表示层绑定控件,逻辑层调用数据层执行存储过程并返回数据,然后想用完数据后关掉,结果虽然没有错误 但是提示SqlHelper1.closeConn(); <——检测到无法访问的代码
我想是不能关掉数据库连接,请哪位高手给出这种架构的最好方案,可以用完数据马上关掉
谢谢先!
我的表示层:
HotProduct1.DataSource=Product.HotProducts();//Bussiness.Product
HotProduct1.DataBind();
逻辑层:
public static SqlDataReader HotProducts()
{
   string ProcedureName="PR_HotProducts";存储过程
   SqlHelper SqlHelper1=new SqlHelper();
   SqlHelper1.OpenConn(); 
   return SqlHelper1.ExecDataReader(ProcedureName);  
   SqlHelper1.closeConn(); <——检测到无法访问的代码
}
数据层:
public SqlDataReader ExecDataReader(string ProcedureName)
{
   SqlCommand myCommand=new SqlCommand(ProcedureName,Conn);
   myCommand.CommandType=CommandType.StoredProcedure;
   SqlDataReader result = myCommand.ExecuteReader();
   return result;
}

解决方案 »

  1.   

    return SqlHelper1.ExecDataReader(ProcedureName);  
    这一句,也就是return 之后的语句不会再执行了,你这样做
    public static SqlDataReader HotProducts()
    {
       string ProcedureName="PR_HotProducts";存储过程
       SqlHelper SqlHelper1=new SqlHelper();
       SqlHelper1.OpenConn(); 
       SqlDataReader rtnReader = SqlHelper1.ExecDataReader(ProcedureName);  
       SqlHelper1.closeConn(); 
       return rtnReader;
    }
    就可以了
      

  2.   

    cmd.ExecuteReader(CommandBehavior.CloseConnection)
    逻辑层:
    public static SqlDataReader HotProducts()
    {
       string ProcedureName="PR_HotProducts";存储过程
       SqlHelper SqlHelper1=new SqlHelper();
       SqlHelper1.OpenConn(); 
       return SqlHelper1.ExecDataReader(ProcedureName);  
       //SqlHelper1.closeConn(); <——检测到无法访问的代码
    }
    数据层:
    public SqlDataReader ExecDataReader(string ProcedureName)
    {
       SqlCommand myCommand=new SqlCommand(ProcedureName,Conn);
       myCommand.CommandType=CommandType.StoredProcedure;
       SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
       return result;
    }
      

  3.   

    return SqlHelper1.ExecDataReader(ProcedureName);  你return之后的代码当然不能执行啊。楼上正解。
      

  4.   

    1。前面都已经Return了,当然不会再执行后面的语句。
    2。就这个具体的方法来说,如果要返回DataReader类型对象,那就不能调用close之类的方法,否则就不会返回结果。只能在使用HotProducts()的地方调用Close()。
      

  5.   


    try{你的代码}
    catch(){}
    finnally{ SqlHelper1.closeConn();}一般有数据库操作都要用到异常处理的吧
      

  6.   

    你RETURN 后还加代码,都返回了,可不访问不到了
      

  7.   

    SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    这样的话用完之后不能关闭连接啊 微软的示例就是这样的
    我这样行不行?
    表示层:
    HotProduct1.DataSource=Product.HotProducts();//Bussiness.Product
    HotProduct1.DataBind();
    Product.HotProducts().Close();//关闭reader
      

  8.   

    dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);  //这里应该是read完自动关闭的
    return dr;
      

  9.   

    加一个关闭函数,在你需要的地方调用
    private void GIA31P1_CloseConnection()
    {

                                string strLog;
    if (this.conn != null) 
    {
    try 
    {
    this.conn.Close();
    this.conn = null;

    catch (SqlException se) 
    {
    strLog =  se.Message;
    }
    finally
    {
    strLog =  " finally";
    } }
    }
      

  10.   

    数据层:
    public SqlDataReader ExecDataReader(string ProcedureName)
    {
       SqlCommand myCommand=new SqlCommand(ProcedureName,Conn);
       myCommand.CommandType=CommandType.StoredProcedure;
       return myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    }表示层:
    IDataReader reader = Product.HotProducts();
    HotProduct1.DataSource=reader;
    HotProduct1.DataBind();
    reader.Close();或者,(因为 DbEnumerator 自动调用Close(),参考我的blog
    http://blog.joycode.com/saucer/archive/2004/07/24/28365.aspx
    )HotProduct1.DataSource=Product.HotProducts();
    HotProduct1.DataBind();
    但也应该考虑一下,把DataReader传到表现层是否恰当,是否应该用业务实体集合
      

  11.   

    http://msdn.microsoft.com/msdnmag/issues/05/05/CuttingEdge/default.aspxhttp://msdn.microsoft.com/msdnmag/issues/05/08/CuttingEdge/default.aspx