一个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
}
}
一个WebForm中连接SQL Server的例子的更多相关文章
- 在unity3d中连接sql server
虽然在Unity3D中能够通过PlayerPrefs类来保存和读取数据,但是一旦数据量增大,仅仅通过代码的方式存取数据,这样的工作量是非常大的.那么如何通过使用Sql Server数据库来存取数据呢? ...
- python 连接sql server
linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...
- Visual Studio 连接 SQL Server 的connectionStringz和
近期C#和数据结构的课程设计多次用到了C#中连接SQL Server数据库的问题,当中涉及到数据库文件的附加和连接问题. 当中最烦人的就是 SqlConnection(String connStr) ...
- 在 myeclipse中进行连接sql server的测试
在 myeclipse中,连接 sql server 用的 url connection 与 java 代码 连接的 url值完全相同. (一下为 java的jdbc连接 sql server 成功的 ...
- ASP.NET 连接 SQL Server 和 Oracle 教程
临近期末,有很多同学都问我怎么关于ASP.NET 连接 SQL Server 和 Oracle 的问题.由于人太多了,我也不能一一去帮忙,就写了这篇博客.希望对大家有用处. 首先,前期准备是要安装数据 ...
- python连接sql server数据库实现增删改查
简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...
- NetBeans连接SQL server数据库教程
不废话,直接开始 1.下载sqljdbc.jar 可以从微软中国官方网站下载 SQLJDBC微软中国 笔者提供一个网盘链接Sqljdbc.jar 4个压缩包视版本选择,SQL 2012 用sqljdb ...
- 在Windows Server 2012 R2中搭建SQL Server 2012故障转移集群
需要说明的是我们搭建的SQL Server故障转移集群(SQL Server Failover Cluster)是可用性集群,而不是负载均衡集群,其目的是为了保证服务的连续性和可用性,而不是为了提高服 ...
- 【转】PowerShell 连接SQL Server 数据库 - ADO.NET
转至:http://www.pstips.net/connect-sql-database.html PowerShell 通过ADO.NET连接SQL Server数据库,并执行SQL脚本.工作中整 ...
随机推荐
- Eclipse配置PyDev插件
安装python解释器 安装PyDev: 首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正常运行,请到Java官 ...
- vimrc常用配置项
设置行号 set nu 设置自动缩进 set autoindent 设置tab占n个字符 set tabstop=n 设置以空格代替tab(因为有部分场合不允许使用tab) set expandtab ...
- Looksery Cup 2015 B. Looksery Party 暴力
B. Looksery Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/pro ...
- WinForm特效:拦截窗体上各个部位的点击
windows窗体的标题栏无法直接通过一些默认的事件来控制,需要了解和WM_NCHITTEST相关的windows消息. 以下示例演示了最简单的效果片断: 他会把客户区和标题栏的效果互换,比如无法按住 ...
- Testin云測公布首份国内应用质量报告:半数APP平均启动时间不合格
Testin云測公布首份国内应用质量报告:半数APP平均启动时间不合格 2014/10/23 · Testin · 实验室报告 日前,Testin云測旗下质量管家Master通过随机取样1605款国内 ...
- Linux VM子系统参数调整
Timesten数据库下的Linux page子系统参数调整 如果Timesten(TT)采用了Durablecommits或是share memory segment被lock的话,那么linux ...
- NopCommerce使用Autofac实现依赖注入
NopCommerce的依赖注入是用的AutoFac组件,这个组件在nuget可以获取,而IOC反转控制常见的实现手段之一就是DI依赖注入,而依赖注入的方式通常有:接口注入.Setter注入和构造函数 ...
- Caching Best Practices--reference
reference:http://java.dzone.com/articles/caching-best-practices There is an irresistible attraction ...
- js实现网站导航的二级下拉菜单
http://www.codesky.net/article/201109/1200js/%E5%AE%9E%E7%94%A8%E5%AF%BC%E8%88%AA%E8%8F%9C%E5%8D%95. ...
- Android进阶笔记18:选用合适的IPC方式
1. 相信大家都知道Android进程间通信方式很多,比如AIDL.Messenger等等,接下来我就总结一下这些IPC方式优缺点. 2. IPC方式的优缺点和适用场景 3. 附加:使用Intent实 ...