.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. myeclipse、eclipse中项目复制后(可能无法访问)注意事项 .

    .myEclipse 复制后修改名称,访问不到项目 这是因为,你只是改了项目的名称,而没有改 下面是解决方法: 方法 1.右击你的项目,选择“properties”,在“type filter tex ...

  2. HttpWebRequest post请求获取webservice void数据信息

    private void button2_Click(object sender, EventArgs e) { Hashtable ht = new Hashtable(); ht["sc ...

  3. PHP 页面编码声明方法(header或meta)

    本文为大家介绍了二种页面编码的声明方法,一种是使用php中的header函数来向客户端发送header头,一种是使用html中的meta标签来生明页面编码,本文分别对他们如何使用进行了说明,感兴趣的同 ...

  4. HDU 1504 Disk Tree

    转载请注明出处:http://blog.csdn.net/a1dark 分析:查了一下这题.发现网上没有什么关于这道题的解题报告.其实题目意思挺好懂的.就是给你一些文件的目录结构.然后让你把它们组合在 ...

  5. HTML <base> 标签

    定义和用法 <base> 标签为页面上的所有链接规定默认地址或默认目标. 通常情况下,浏览器会从当前文档的 URL 中提取相应的元素来填写相对 URL 中的空白. 使用 <base& ...

  6. mysql sort 性能优化

    http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html 这段时间mysql 数据库的性能明显降低,iowait达到了30, ...

  7. cocos2d-x中CCCallFunc CCCallFuncN CCCallFuncND的区别和使用示例

    转自:http://xiandanboke.com.cn/cocos2d-xcccallfunc.html CCCallFunc CCCallFuncN CCCallFuncND的区别和使用 CCCa ...

  8. IOS开发UI篇--使用CAShapeLayer实现复杂的View的遮罩效果

    一.案例演示 最近在整理一个聊天的项目的时候,发送图片的时候,会有一个三角的指向效果,指向这张图片的发送者.服务端返回给我们的图片只是一张矩形的图片,我们如何把一张矩形的图片或者View,加上一层自定 ...

  9. MySQL(22):事务管理之 事务回滚

    1. 在操作事务的时候,如果发现当前事务操作是不合理的,此时只要还没有提交事务,就可以通过回滚取消当前事务,接下来就针对事务的回滚进行详细讲解. 2. 为了演示回滚操作,在上一个笔记案例基础之上,此时 ...

  10. Java基础知识强化之IO流笔记76:NIO之 Channel(通道)之间的数据传输

    1. 在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel(译者注:channel中文常译作通道)传输到另外一个channel. (1)trans ...