我新手!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>
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>
{
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的项目组。
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(); 看行不行啊?????
/// <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);
}
}