一个WebForm中连接SQL Server的例子
.cs
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;
using System.Data.SqlClient;
namespace WebApplication1
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnLast;
protected System.Web.UI.WebControls.CheckBox chk1;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblPageCount;
ICollection CreateDataSource()
{
// System.Data.SqlClient.SqlDataAdapter
/*
读取数据库的信息,获得DataView
*/
SqlConnection MyConnection = new SqlConnection("data source=172.16.36.222;initial catalog=RemoteEdu;password=1234567890;persist s" +
"ecurity info=True;user id=sa;workstation id=BAIHAO;packet size=4096");
SqlCommand MyDataSetCommand = new SqlCommand("SELECT GroupID, GroupName, Brief, RegistDate, GroupState, InvalidDate, Deleteable" +
" FROM GroupInfo",MyConnection);
DataSet ds= new DataSet();
SqlDataAdapter ada = new SqlDataAdapter(MyDataSetCommand); //
ada.Fill(ds,"admin_enter");
return ds.Tables["admin_enter"].DefaultView;
}
//然后中是Page_Load函数,在这里主要是判断一下是否显示DataGrid自带的那些分页数字,使用的是PageStyle的Visible属性:
void Page_Load(Object sender, EventArgs e)
{
//判断是否隐藏PagerStyle-Mode
if (chk1.Checked)
{
MyDataGrid.PagerStyle.Visible=true;
}
else
{
MyDataGrid.PagerStyle.Visible=false;
}
BindGrid();
}
//下面是处理点击事件的PagerButtonClick,这是我们的核心部分,其实我们操作的也只是DataGrid的CurrentPageIndex属性。如果CurrentPageIndex小于PageCount则有下一页,如果CurrentPageIndex大于0则表示有前一页。
protected void PagerButtonClick(Object sender, EventArgs e)
{
//获得LinkButton的参数值
String arg = ((LinkButton)sender).CommandArgument;
switch(arg)
{
case ("next"):
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
MyDataGrid.CurrentPageIndex ++;
break;
case ("prev"):
if (MyDataGrid.CurrentPageIndex > 0)
MyDataGrid.CurrentPageIndex --;
break;
case ("last"):
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
//本页值
MyDataGrid.CurrentPageIndex = Int32.Parse(arg);
break;
}
BindGrid();
}
//下面是MyDataGrid_Page,主要操作是调用BindGrid函数,以将数据交给DataGrid显示:
protected void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{
//处理按下数字的方法
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
//最后是两个函数,他们的作用,我都注释了:)
void BindGrid()
{
//将DataView绑定到DataGrid上去
MyDataGrid.DataSource = CreateDataSource();
MyDataGrid.DataBind();
ShowStats();
}
void ShowStats()
{
//显示页面信息
lblCurrentIndex.Text = "当前页数为: " + ((int)MyDataGrid.CurrentPageIndex+1);
lblPageCount.Text = "总页数是: " + MyDataGrid.PageCount;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}