.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
}
}

一个WebForm中连接SQL Server的例子的更多相关文章

  1. 在unity3d中连接sql server

    虽然在Unity3D中能够通过PlayerPrefs类来保存和读取数据,但是一旦数据量增大,仅仅通过代码的方式存取数据,这样的工作量是非常大的.那么如何通过使用Sql Server数据库来存取数据呢? ...

  2. python 连接sql server

    linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...

  3. Visual Studio 连接 SQL Server 的connectionStringz和

    近期C#和数据结构的课程设计多次用到了C#中连接SQL Server数据库的问题,当中涉及到数据库文件的附加和连接问题. 当中最烦人的就是  SqlConnection(String connStr) ...

  4. 在 myeclipse中进行连接sql server的测试

    在 myeclipse中,连接 sql server 用的 url connection 与 java 代码 连接的 url值完全相同. (一下为 java的jdbc连接 sql server 成功的 ...

  5. ASP.NET 连接 SQL Server 和 Oracle 教程

    临近期末,有很多同学都问我怎么关于ASP.NET 连接 SQL Server 和 Oracle 的问题.由于人太多了,我也不能一一去帮忙,就写了这篇博客.希望对大家有用处. 首先,前期准备是要安装数据 ...

  6. python连接sql server数据库实现增删改查

    简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...

  7. NetBeans连接SQL server数据库教程

    不废话,直接开始 1.下载sqljdbc.jar 可以从微软中国官方网站下载 SQLJDBC微软中国 笔者提供一个网盘链接Sqljdbc.jar 4个压缩包视版本选择,SQL 2012 用sqljdb ...

  8. 在Windows Server 2012 R2中搭建SQL Server 2012故障转移集群

    需要说明的是我们搭建的SQL Server故障转移集群(SQL Server Failover Cluster)是可用性集群,而不是负载均衡集群,其目的是为了保证服务的连续性和可用性,而不是为了提高服 ...

  9. 【转】PowerShell 连接SQL Server 数据库 - ADO.NET

    转至:http://www.pstips.net/connect-sql-database.html PowerShell 通过ADO.NET连接SQL Server数据库,并执行SQL脚本.工作中整 ...

随机推荐

  1. 用javascript实现简体和繁体字间的转换

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  2. 开源图形库 c语言-图形图像库 集合[转]

    开源图形库 c语言-图形图像库 集合[转] Google三维API O3D O3D 是一个开源的 Web API 用来在浏览器上创建界面丰富的交互式的 3D 应用程序.这是一种基于网页的可控3D标准. ...

  3. (hdu step 8.1.1)ACboy needs your help again!(STL中栈和队列的基本使用)

    题目: ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...

  4. Using UTL_DBWS to Make a Database 11g Callout to a Document Style Web Service

    In this Document   _afrLoop=100180147230187&id=841183.1&displayIndex=2&_afrWindowMode=0& ...

  5. SQL SERVER中查找某关键词位于哪些存储过程或函数

    USE [MYDB] go SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE routine_type='PROCEDURE' AND routine_d ...

  6. 基于jQuery上下切换的焦点图—带缩略图悬浮

    分享一款基于jQuery上下切换的焦点图,这款焦点图带有缩略图悬浮,它的切换效果比较简单,仅仅是作图片的上下切换,但是效果还是比较流畅的.这款jQuery焦点图插件的另外一个特点是在播放器上面可以悬浮 ...

  7. Docker 1.12.0将要发布的新功能

    Docker 1.12.0将要发布的新功能 导读 按计划,6/14 是1.12.0版本的 feature冻结 的日子,再有两个星期Docker 1.12.0也该发布了.这里列出来的新功能,都是已经合并 ...

  8. flume监控之ganglia

    对于日志来说,我觉得监控意义不大,因为写的速度一般不会特别快,但是如果是spooldir source,里面一小时放入十几G的数据让flume解析,特别是在结合kafka或者其他框架的话,监控就显得重 ...

  9. IIS 之 HTTP 错误 404.3 - Not Found(由于扩展配置问题而无法提供您请求的页面...)

    错误如下图所示: 其实在IIS7中肯定能支持的的,只是我们在Win7中安装IIS7的时候没有勾选这些功能,具体方法如下: 1.依次打开" 控制面板→程序→程序和功能→打开或关闭Windwos ...

  10. solr4.x配置IK2012FF智能分词+同义词配置

    本文配置环境:solr4.6+ IK2012ff +tomcat7 在Solr4.0发布以后,官方取消了BaseTokenizerFactory接口,而直接使用Lucene Analyzer标准接口T ...