我新手!1?帮我把下面的添上,能正常浏览Default.aspx.
2?connDB这个类和Default.aspx在不在一个目录有什么关系吗?(比如App_code/connDB.cs)
public class connDB

public connDB()
{ }
}
下面是想要继承的Default.aspx.cs
public partial class _Default : System.Web.UI.Page ??
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
           
        OleDbCommand cmd = new OleDbCommand("select * from type",??);        
        OleDbDataReader dr = cmd.ExecuteReader();
        this.GridView2.DataSource = dr;
        this.GridView2.DataBind();        
        
    }
}
Web.config配制
<connectionStrings>
  <add name="mydataConnectionString" connectionString="App_Data/mydata.mdb"       
   providerName="System.Data.OleDb" />
    <add name="provider" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=??"/>      </connectionStrings>

解决方案 »

  1.   

    没有这个功能,.net不支持多重继承!一种办法是:把connDB接口定义设置成接口 IConnDB,然后让所有页面都使用这个IConnDB,再在页面内实例化一个connDB对象,最后在页面内所有IConnDB方法中将操作委派给connDB类型的内部对象操作。这种用接口来模拟的继承是非常愚蠢的,当你在开发维护connDB的时候,每当接口改变,所有使用它的子类全都会编译或者运行出现异常,并且大量操作委派代码完全是hard copy非常痛苦。只有看在实现了多重继承的份上,看在有利于多态的份上,可以使用接口。还有一种办法,如果你根本不需要把它作为接口,那么就使用单独的类型甚至把它看作函数库,在页面中组合进他们就行了。你可以写这样一个类:public class myPageHelper
    {
      static public InitView(Page th)
      {
         OleDbCommand cmd = new OleDbCommand("select * from type",??);        
         OleDbDataReader dr = cmd.ExecuteReader();
         th.GridView2.DataSource = dr;
         th.GridView2.DataBind();        
      }
    }然后在你的页面中使用 myPageHelper.InitView(this) 来共享这段代码。当看到微软的没有例子中有大量的 xxxHelper、xxxManager之类的命名的类型的时候,我都会习惯意识到开发这个系统的项目组又是个用OOPL但是并不懂OOAD的项目组。
      

  2.   

    你把方法声明成static不就方便了?不用那么麻烦把
      

  3.   

    尽管我应该承认使用接口来模拟继承会写一大堆废话来处理接口硬性规定的语法,并且接口还在你修改结构的高层次定义的时候会带来巨大的痛苦,尽管静态函数是那么好用,我还是建议你如果你第一个意识中就把你的需求设计为可继承的基类,最好还是坚持用接口来把你的 IConnDB 用到每一个page上,这样抛开编程单看设计(设计中不区分是class还是interface)与潜意识中的需求比较接近。当然也可以退一步,不再坚持 connDB 这个概念,转而认为有一种特殊 page ,然后让你的页面的后台类型都从这个特殊的page继承,这样在.net这种单继承语言中也能使用继承来贯彻一部分需求。只不过你不能认为connDB是个基类。
      

  4.   

    如果能够想到 connDB 作为基类,最后由于 .net 在多重继承上功能太弱而不得不放弃面向对象设计方法,这其实经常遇到。框架设计者提出这类超越编码工具的狭义表现方式的设计来是很有好处的。
      

  5.   

    在类 connDB中加上下面的一个方法,试试看:先把这个也加上:
    protected static string connectionString = ConfigurationSettings.AppSettings["provider"];
                      /// <summary>
    /// 执行查询语句,返回SqlDataReader
    /// </summary>
    /// <param name="strSQL">查询语句</param>
    /// <returns>SqlDataReader</returns>
    public static SqlDataReader ExecuteReader(string strSQL)
    {
    SqlConnection connection = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(strSQL,connection);
    try
    {
    connection.Open();
    SqlDataReader myReader = cmd.ExecuteReader();
    return myReader;
    }
    catch(System.Data.SqlClient.SqlException e)
    {
    throw new Exception(e.Message);
    }

    } 在那个default.aspx页面中,
     OleDbDataReader dr = connDB.ExecuteReader("select * from type");
            this.GridView2.DataSource = dr;
            this.GridView2.DataBind();    看行不行啊?????
      

  6.   

    错了,错了。我搞错了,你要的是 Access 的,我发的是sql server 的。
    /// <summary>
    /// 执行查询语句,返回SqlDataReader
    /// </summary>
    /// <param name="strSQL">查询语句</param>
    /// <returns>SqlDataReader</returns>
    public static OleDbDataReader ExecuteReader(string strSQL)
    {
    OleDbConnection connection = new OleDbConnection(connectionString);
    OleDbCommand cmd = new OleDbCommand(strSQL,connection);
    try
    {
    connection.Open();
    OleDbDataReader myReader = cmd.ExecuteReader();
    return myReader;
    }
    catch(System.Data.OleDb.OleDbException e)
    {
    throw new Exception(e.Message);
    }

    }