既然你的connection类只是返回一个conn,为什么不在开始的Connection conn=new Connection()后添加SqlConnection conn1=conn.conn();以后直接对conn1操作,至少容易懂哈。
给你篇数据库类的参考:(zz)
using System;
using System.Data;
using System.Data.OleDb;namespace DbClass
{
 /// <summary>
 /// Db_Class 的摘要说明。
 /// </summary>
 public class Db_Class
 {
  public OleDbConnection Conn;
  //构造函数
  public Db_Class()
  {
   Conn= new OleDbConnection("Provider=SQLOLEDB;Server=(local);Pwd=123456;UID=sa;Database=test");
  }
  //打开数据源链接
  public OleDbConnection Db_Conn()
  {
   Conn.Open();
   return Conn;
  }
  //返回DataReader数据集,下面的SQL可以动态生成
  public OleDbDataReader Db_CreateReader(string SQL)
  {
           Db_Conn();
     OleDbCommand cmd = new OleDbCommand(SQL,Conn);
     OleDbDataReader Rs = cmd.ExecuteReader();
     return Rs;
     this.close();
  }
  //返回DataReader数据集,下面的SQL是存储过程
  public OleDbDataReader Db_CommandReader(string SQL)
  {
   Db_Conn();
   OleDbCommand cmd = new OleDbCommand(SQL,Conn);
   cmd.CommandType = CommandType.StoredProcedure;
   OleDbDataReader Rs = cmd.ExecuteReader();
   return Rs;
   this.close();
  }
  //返回数据DataSet数据集
  public OleDbDataSet Db_CreateDataSet(string SQL)
  {
   Db_Conn();
   OleDbCommand cmd = new OleDbCommand(SQL,Conn);
   OleDbDataAdapter Adpt= new OleDbDataAdapter(cmd,Conn);
   DataSet Ds = new DataSet();
   Adpt.Fill(Ds,"NewTable");
   return Ds;
   this.close();
  }
  //返回数据DataReader数据集,不需要返回数据的修改,删除可以使用本函数
  public bool Db_ExecuteNonquery(string SQL)
  {
            Db_Conn();
            OleDbCommand cmd = new OleDbCommand(SQL,Conn);
   try
   {
    OleDbDataReader Rs= cmd.ExecuteNonQuery();
    return true;
   }
   catch
   {
    return false;
   }
   this.close();
  }
        //关闭数据链接
  public void close()
  {
   Conn.Close();
  } }
}
使用方法如下:using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace DbClass
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   //string SQL="select * from sysfiles";
   Db_Class Db_class = new Db_Class();
   DataGrid1.DataSource=Db_class.Db_CommandReader("sp_tables");//使用SQLSERVER的存储过程。
   DataGrid1.DataBind();
  }  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {    
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
   this.Load += new System.EventHandler(this.Page_Load);  }
  #endregion
 }
}

解决方案 »

  1.   

    mydr=mycommand.ExecuteReader(CommandBehavior.CloseConnection);
      

  2.   

    我的  Connection
     类 是一个单独 的DLL 的。
      

  3.   

    to: gshope(北京.Net) 强啊,大哥你太强了。我改成
    mydr=mycommand.ExecuteReader(CommandBehavior.CloseConnection);
    后 F5 刷的手都酸了,也没有错误了,谢谢啊。顺便问一下,CommandBehavior.CloseConnection 也是在关闭 datareader 后自动关闭连接,那么我 
    mydr.Close();
    conn.Conn().Close();
    conn.Conn().Dispose();这几句也关闭连接了啊??为什么没有用??他们用法有什么区别?
      

  4.   

    你的Connection类每次调用conn()时都会新建一个connection对象,
    所以你的conn.conn().Close()语句功能就是新建连接,打开连接然后关闭连接。SqlCommand mycommand=new SqlCommand("select * from admin_user",conn.Conn());
    这个语句初始化了SqlComand对象的CommandText和Connection属性。所以你执行完mydr=mycommand.ExecuteReader();
    while (mydr.Read())
    {
    Response.Write(mydr["id"]+"<br>");
    }
    这段代码后应该这样关闭对象
    mydr.Close();
    myCommand.Connection.Close();
      

  5.   

    mydr=mycommand.ExecuteReader();
    ======================>
    mydr=mycommand.ExecuteReader(CommandBehavior.CloseConnection);当正在使用 SqlDataReader 时,关联的 SqlConnection 在忙于为 SqlDataReader 提供服务。当处于此状态时,除了关闭 SqlConnection 外,不能对其执行其他任何操作。在调用 SqlDataReader.Close 方法之前一直如此。如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection,则关闭 SqlDataReader 会自动关闭此连接。
      

  6.   

    各位兄弟,谢谢了,以后搞 DATAREADER 看来都要 mydr=mycommand.ExecuteReader(CommandBehavior.CloseConnection);这样了。。呵呵
      

  7.   

    各位兄弟,谢谢了,以后搞 DATAREADER 看来都要 mydr=mycommand.ExecuteReader(CommandBehavior.CloseConnection);这样了。。呵呵