App_Code 使用方法 I
Default.aspx |
<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" /> </div> </form> |
Default.aspx.cs |
using WebApplication1.App_Code; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GridView1.DataSource = Class1.GetData(); GridView1.DataBind(); } } } |
App_Code\Class1.cs |
using System.Data; using System.Data.SqlClient; using System.Text; namespace WebApplication1.App_Code { public class Class1 { public static DataView GetData() { string connectionString1 = "Data Source=.\\SqlExpress; Initial Catalog=soccer; Integrated Security=SSPI"; SqlConnection con1 = new SqlConnection(connectionString1); con1.Open(); StringBuilder sb = new StringBuilder(); sb.Append("SELECT * FROM dbo.Team ORDER BY Team_ID;"); SqlDataAdapter Adapter = new SqlDataAdapter(sb.ToString(), con1); DataSet ds = new DataSet(); Adapter.Fill(ds); con1.Close(); con1.Dispose(); return ds.Tables[0].DefaultView; } public static DataView GetData2(string procedure, int from, int to) { string connectionString1 = "Data Source=.\\SqlExpress; Initial Catalog=soccer; Integrated Security=SSPI"; SqlConnection con1 = new SqlConnection(connectionString1); con1.Open(); StringBuilder sb = new StringBuilder(); sb.Append("EXEC dbo."); sb.Append(procedure); sb.Append(" "); sb.Append((from-1).ToString()); sb.Append(" "); sb.Append(","); sb.Append((to).ToString()); sb.Append(";"); SqlDataAdapter Adapter = new SqlDataAdapter(sb.ToString(), con1); DataSet ds = new DataSet(); Adapter.Fill(ds); con1.Close(); con1.Dispose(); return ds.Tables[0].DefaultView; } } } |