这是在下经心为初学三层架构的同志打造的例子,一定会对你深入的理解这个架构
有一定的帮助,在这个例子中还用到的分页的存储过程,用的是Sql2005里新加的Rownumber()函数,是最好最简单的分页,加上前台的AspNetPager实现的分页效果,你还等什么来学一下吧,附有源代码可以下载,下载直接要吧运行,
所需的环境:VS2008+sql2005
代码只开放下面的这么多,大家有什么不明白的就留言问我,也可以给我要源代码呵呵
http://d.download.csdn.net/down/1106433/sufei1013
1.先来看一张实现后的图片吧
这 是实现后的效果,还不错吧,上面的分页效果一定和你想要的差不多吧,怎么实现的呢,来和我一起学习一下吧,
2.首先我们要写一个分页的存储过程
Code
USE [base]
GO
/****** 对象: StoredProcedure [dbo].[sufeiAll] 脚本日期: 03/10/2009 10:03:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sufeiAll]
@startIndex INT, --这里是当前的页码
@pageSize INT --这里是分页的大小
AS
with SClass as( --生成一个新的表,表中的数据是从()里得到的,写起来也方便,只要把想要的写在()里就OK了
select row_number() over(order by userName) as rownumber,--一个新的行是从1到N
userName,pwd,sex,age
from sufei )
/**上半句产生一个临时表,表名为SClass,其内容来自于自查询select row_number().**/
select userName,pwd,sex,age
from SClass
where rownumber >=(@startindex-1)*@pagesize+1 and rownumber<=(@startindex-1)*@pagesize+(@pagesize)
select count(*) from dbo.sufei--返回所有的行
---在这个存储过程里一共会返回两个表,第一个是所有的数据,第二个是总记录数
USE [base]
GO
/****** 对象: StoredProcedure [dbo].[sufeiAll] 脚本日期: 03/10/2009 10:03:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sufeiAll]
@startIndex INT, --这里是当前的页码
@pageSize INT --这里是分页的大小
AS
with SClass as( --生成一个新的表,表中的数据是从()里得到的,写起来也方便,只要把想要的写在()里就OK了
select row_number() over(order by userName) as rownumber,--一个新的行是从1到N
userName,pwd,sex,age
from sufei )
/**上半句产生一个临时表,表名为SClass,其内容来自于自查询select row_number().**/
select userName,pwd,sex,age
from SClass
where rownumber >=(@startindex-1)*@pagesize+1 and rownumber<=(@startindex-1)*@pagesize+(@pagesize)
select count(*) from dbo.sufei--返回所有的行
---在这个存储过程里一共会返回两个表,第一个是所有的数据,第二个是总记录数
点上面的链接要吧查询看存储过程
表非常的简单只有四个字段,全是Varchar(50)
看过了上面的存储过程我们下面要做的就是怎么来用他
3.我们来搭建一个我们的MVc吧,
第一层,DAo层
里面有三个类第一个
首先来看一张图说明一下三层的样子,呵呵
下面我给出三层的代码
EmployeesDao层
1.SqlDBHelper.cs
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace EmployeesDao
{
/**//// 功能(Function):存放对数据库的访问方法、变量
/// 编写日期 2009-2-21
/// 编写人sufei
/// <summary>
/// 访问数据所用的代码,此类为一个抽象的类
/// 不能实例化,真接用类名调用即可
/// </summary>
public abstract class SqlDBHelper
{
//获取数据库连接字符串,其属于静态变量且只读,项目中所有文档可以直接使用,但不能修改
public static readonly string sufeiConncetin = "Data Source=sufei\\SQLEXPRESS;Initial Catalog=base;Integrated Security=true";
/**//// <summary>
///执行一个不需要返回值的SqlCommand命令,通过指定专用的连接字符串。
/// 使用参数数组形式提供参数列表
/// </summary>
/// <remarks>
/// 使用示例:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">SqlCommand命令类型 (存储过程, T-SQL语句, 等等。)</param>
/// <param name="commandText">存储过程的名字或者 T-SQL 语句</param>
/// <param name="commandParameters">以数组形式提供SqlCommand命令中用到的参数列表</param>
/// <returns>返回一个数据值表示此SqlCommand命令执行后影响的行数</returns>
public static int ExecteNonQuery(string conncetionString, CommandType cmdType, string cmdText, params SqlParameter [] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(conncetionString))
{
//通过PrePareCommand方法将参数逐个传入到SqlCommand的参数集合中
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
//清空SqlCommand中的参数列表
cmd.Parameters.Clear();
return val;
}
}
/**//// <summary>
/// 执行一条返回结果集的SqlCommand,通过一个已经存在的数据库连接
/// 使用参数数组提供参数
/// </summary>
/// <remarks>
/// 使用示例:
/// DataTable table=GetTable(conn,CommandType.StoredProcedure,"PublishOrders");
/// </remarks>
/// <param name="connecttionString">一个现有的数据库连接</param>
/// <param name="cmdTye">SqlCommand命令类型</param>
/// <param name="cmdText">存储过程的名字或者 T-SQL 语句</param>
/// <returns>返回一个表(DataTable)表示查询得到的数据集</returns>
public static DataTableCollection GetTable(string connecttionString, CommandType cmdTye, string cmdText,params SqlParameter [] commandParameters)
{
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connecttionString))
{
PrepareCommand(cmd, conn, null, cmdTye, cmdText,commandParameters);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
}
//返回一个表集
return ds.Tables;
}
/**////<summary>
///为执行命令准备参数
///</summary>
///<param name="cmd">Sqlcommang命令</param>
///<param name="conn">已经存在的数据库连接</param>
///<param name="trans">数据库事物处理</param>
///<param name="cmdType">SqlCommand命令类型(存储过程,T-Sql语句,等等。)</param>
///<param name="cmdText">Command text,T-Sql语句,例如:Select * from sufei</param>
///<param name="cmdParms">返回带参数的命令</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
//判断数据库连接状态
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
//判断是否需要事物处理
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace EmployeesDao
{
/**//// 功能(Function):存放对数据库的访问方法、变量
/// 编写日期 2009-2-21
/// 编写人sufei
/// <summary>
/// 访问数据所用的代码,此类为一个抽象的类
/// 不能实例化,真接用类名调用即可
/// </summary>
public abstract class SqlDBHelper
{
//获取数据库连接字符串,其属于静态变量且只读,项目中所有文档可以直接使用,但不能修改
public static readonly string sufeiConncetin = "Data Source=sufei\\SQLEXPRESS;Initial Catalog=base;Integrated Security=true";
/**//// <summary>
///执行一个不需要返回值的SqlCommand命令,通过指定专用的连接字符串。
/// 使用参数数组形式提供参数列表
/// </summary>
/// <remarks>
/// 使用示例:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一个有效的数据库连接字符串</param>
/// <param name="commandType">SqlCommand命令类型 (存储过程, T-SQL语句, 等等。)</param>
/// <param name="commandText">存储过程的名字或者 T-SQL 语句</param>
/// <param name="commandParameters">以数组形式提供SqlCommand命令中用到的参数列表</param>
/// <returns>返回一个数据值表示此SqlCommand命令执行后影响的行数</returns>
public static int ExecteNonQuery(string conncetionString, CommandType cmdType, string cmdText, params SqlParameter [] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(conncetionString))
{
//通过PrePareCommand方法将参数逐个传入到SqlCommand的参数集合中
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
//清空SqlCommand中的参数列表
cmd.Parameters.Clear();
return val;
}
}
/**//// <summary>
/// 执行一条返回结果集的SqlCommand,通过一个已经存在的数据库连接
/// 使用参数数组提供参数
/// </summary>
/// <remarks>
/// 使用示例:
/// DataTable table=GetTable(conn,CommandType.StoredProcedure,"PublishOrders");
/// </remarks>
/// <param name="connecttionString">一个现有的数据库连接</param>
/// <param name="cmdTye">SqlCommand命令类型</param>
/// <param name="cmdText">存储过程的名字或者 T-SQL 语句</param>
/// <returns>返回一个表(DataTable)表示查询得到的数据集</returns>
public static DataTableCollection GetTable(string connecttionString, CommandType cmdTye, string cmdText,params SqlParameter [] commandParameters)
{
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connecttionString))
{
PrepareCommand(cmd, conn, null, cmdTye, cmdText,commandParameters);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
}
//返回一个表集
return ds.Tables;
}
/**////<summary>
///为执行命令准备参数
///</summary>
///<param name="cmd">Sqlcommang命令</param>
///<param name="conn">已经存在的数据库连接</param>
///<param name="trans">数据库事物处理</param>
///<param name="cmdType">SqlCommand命令类型(存储过程,T-Sql语句,等等。)</param>
///<param name="cmdText">Command text,T-Sql语句,例如:Select * from sufei</param>
///<param name="cmdParms">返回带参数的命令</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
//判断数据库连接状态
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
//判断是否需要事物处理
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}
2.SqlFiled.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/**//// <summary>
///SqlFiled 的摘要说明
/// </summary>
namespace EmployeesDao
{
public class SqlFiled
{
/**//// <summary>
/// sufei表用于存放一些测试信息
/// </summary>
/// <summary>
/// sufei
/// </summary>
public static string userName="@userName",pwd="@pwd",sex="@sex",age="@age";
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/**//// <summary>
///SqlFiled 的摘要说明
/// </summary>
namespace EmployeesDao
{
public class SqlFiled
{
/**//// <summary>
/// sufei表用于存放一些测试信息
/// </summary>
/// <summary>
/// sufei
/// </summary>
public static string userName="@userName",pwd="@pwd",sex="@sex",age="@age";
}
}
3.SqlSTring.cs
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EmployeesDao
{
public class SqlSTring
{
//查询所有的Manage#region//查询所有的Manage
/**//// <summary>
///删除一个管理员
/// </summary>
public static string selectdelete = @"selectDelete";
/**////<summary>
///查询所有
///</summary>
public static string sufeiAll = @"sufeiAll";
/**////<summary>
///添加一个用户
///</summary>
public static string sufeiOneInsert = @"sufei_Insert";
/**////<summary>
///根据用户名查询一个用户的详细信息
///</summary
public static string sufeiOneSelectByuserName = @"sufeiOneSelectByuserName";
///<summary>
///根据用户名修改相应的用户信息
///</summary
public static string sufeiOneUpdateByuserName = @"sufeiOneUpdateByuserName";
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EmployeesDao
{
public class SqlSTring
{
//查询所有的Manage#region//查询所有的Manage
/**//// <summary>
///删除一个管理员
/// </summary>
public static string selectdelete = @"selectDelete";
/**////<summary>
///查询所有
///</summary>
public static string sufeiAll = @"sufeiAll";
/**////<summary>
///添加一个用户
///</summary>
public static string sufeiOneInsert = @"sufei_Insert";
/**////<summary>
///根据用户名查询一个用户的详细信息
///</summary
public static string sufeiOneSelectByuserName = @"sufeiOneSelectByuserName";
///<summary>
///根据用户名修改相应的用户信息
///</summary
public static string sufeiOneUpdateByuserName = @"sufeiOneUpdateByuserName";
#endregion
}
}
下面是EmployeesModel层
在这里只有一个叫sufei.cs的类是一个实体类,主要是用来传值的
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
///sufei 的摘要说明
/// </summary>
namespace EmployeesModel
{
public class sufei
{
//封装姓名
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
//封装密码
private string pwd;
public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
//封装性
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
//封装年龄
private string age;
public string Age
{
get { return age; }
set { age = value; }
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
///sufei 的摘要说明
/// </summary>
namespace EmployeesModel
{
public class sufei
{
//封装姓名
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
//封装密码
private string pwd;
public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
//封装性
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
//封装年龄
private string age;
public string Age
{
get { return age; }
set { age = value; }
}
}
}
下面的Bll层这里面写的是所有的访问方法
sufeiServers.cs
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using EmployeesDao;
using EmployeesModel;
using System.Data.SqlClient;
namespace EmployeesBLL
{
public class sufeiServers
{
/**//// <summary>
/// 获取所有管理员(编号,姓名,密码,权限,状态)信息
/// </summary>
/// <returns>所有管理员(编号,姓名,密码,权限,状态)信息</returns>
public DataTableCollection GetAllSufei(int startIndex, int pageSize)
{
//添加两个参数
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter("@startIndex" ,SqlDbType.Int),
new SqlParameter("@pageSize",SqlDbType.Int)
};
//给参赋值
parms[0].Value = startIndex;
parms[1].Value = pageSize;
DataTableCollection table = SqlDBHelper.GetTable(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.sufeiAll,parms);
return table;
}
/**//// <summary>
/// 根据用户的用户名删除用户的信息
/// </summary>
/// <returns>如果返回为true删除成功,如果返回为false删除失败</returns>
public Boolean DeleteOneSufei(sufei objsufei)
{
//添加一个参数
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter(SqlFiled.userName ,SqlDbType.VarChar,11)
};
//给参赋值
parms[0].Value = objsufei.UserName;
int result = SqlDBHelper.ExecteNonQuery(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.selectdelete, parms);
if (result > 0)
{
return true;
}
else
{
return false;
}
}
/**//// <summary>
/// 插入一条数据
/// </summary>
/// <returns>如果返回为true添加成功,如果返回为false添加失败</returns>
public Boolean InserSufei(sufei objsufei)
{
//添加四个参数
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter (SqlFiled .userName ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .pwd ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .sex ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .age ,SqlDbType .VarChar ,50)
};
parms[0].Value = objsufei .UserName;
parms[1].Value = objsufei .Pwd ;
parms[2].Value = objsufei .Sex ;
parms[3].Value = objsufei .Age ;
int result = SqlDBHelper.ExecteNonQuery(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.sufeiOneInsert, parms);
if (result > 0)
{
return true;
}
else
{
return false;
}
}
/**//// <summary>
/// 查询一条数据
/// </summary>
/// <returns>返回一个DataTable代表的是数据的集合</returns>
public DataTable sufeiOneSelect(sufei objsufei)
{
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter (SqlFiled .userName ,SqlDbType .VarChar ,50)
};
parms[0].Value = objsufei.UserName;
DataTable objdable = SqlDBHelper.GetTable(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.sufeiOneSelectByuserName, parms)[0];
return objdable;
}
/**//// <summary>
/// 更新一条数据
/// </summary>
/// <returns>如果返回为true修改成功,如果返回为false修改失败</returns>
public Boolean sufeiOneUpdate(sufei objsufei)
{
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter(SqlFiled .userName ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .pwd ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .sex,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .age ,SqlDbType .VarChar ,50)
};
parms[0].Value = objsufei.UserName;
parms[1].Value = objsufei.Pwd;
parms[2].Value = objsufei.Sex;
parms[3].Value = objsufei.Age;
int result = SqlDBHelper.ExecteNonQuery(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.sufeiOneUpdateByuserName, parms);
if (result > 0)
{
return true;
}
else
{
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using EmployeesDao;
using EmployeesModel;
using System.Data.SqlClient;
namespace EmployeesBLL
{
public class sufeiServers
{
/**//// <summary>
/// 获取所有管理员(编号,姓名,密码,权限,状态)信息
/// </summary>
/// <returns>所有管理员(编号,姓名,密码,权限,状态)信息</returns>
public DataTableCollection GetAllSufei(int startIndex, int pageSize)
{
//添加两个参数
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter("@startIndex" ,SqlDbType.Int),
new SqlParameter("@pageSize",SqlDbType.Int)
};
//给参赋值
parms[0].Value = startIndex;
parms[1].Value = pageSize;
DataTableCollection table = SqlDBHelper.GetTable(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.sufeiAll,parms);
return table;
}
/**//// <summary>
/// 根据用户的用户名删除用户的信息
/// </summary>
/// <returns>如果返回为true删除成功,如果返回为false删除失败</returns>
public Boolean DeleteOneSufei(sufei objsufei)
{
//添加一个参数
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter(SqlFiled.userName ,SqlDbType.VarChar,11)
};
//给参赋值
parms[0].Value = objsufei.UserName;
int result = SqlDBHelper.ExecteNonQuery(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.selectdelete, parms);
if (result > 0)
{
return true;
}
else
{
return false;
}
}
/**//// <summary>
/// 插入一条数据
/// </summary>
/// <returns>如果返回为true添加成功,如果返回为false添加失败</returns>
public Boolean InserSufei(sufei objsufei)
{
//添加四个参数
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter (SqlFiled .userName ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .pwd ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .sex ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .age ,SqlDbType .VarChar ,50)
};
parms[0].Value = objsufei .UserName;
parms[1].Value = objsufei .Pwd ;
parms[2].Value = objsufei .Sex ;
parms[3].Value = objsufei .Age ;
int result = SqlDBHelper.ExecteNonQuery(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.sufeiOneInsert, parms);
if (result > 0)
{
return true;
}
else
{
return false;
}
}
/**//// <summary>
/// 查询一条数据
/// </summary>
/// <returns>返回一个DataTable代表的是数据的集合</returns>
public DataTable sufeiOneSelect(sufei objsufei)
{
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter (SqlFiled .userName ,SqlDbType .VarChar ,50)
};
parms[0].Value = objsufei.UserName;
DataTable objdable = SqlDBHelper.GetTable(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.sufeiOneSelectByuserName, parms)[0];
return objdable;
}
/**//// <summary>
/// 更新一条数据
/// </summary>
/// <returns>如果返回为true修改成功,如果返回为false修改失败</returns>
public Boolean sufeiOneUpdate(sufei objsufei)
{
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter(SqlFiled .userName ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .pwd ,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .sex,SqlDbType .VarChar ,50),
new SqlParameter (SqlFiled .age ,SqlDbType .VarChar ,50)
};
parms[0].Value = objsufei.UserName;
parms[1].Value = objsufei.Pwd;
parms[2].Value = objsufei.Sex;
parms[3].Value = objsufei.Age;
int result = SqlDBHelper.ExecteNonQuery(SqlDBHelper.sufeiConncetin, CommandType.StoredProcedure, SqlSTring.sufeiOneUpdateByuserName, parms);
if (result > 0)
{
return true;
}
else
{
return false;
}
}
}
}
这里给出 是的一个前台的界面,有助于大家的学习
Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EmployeesBLL;
using EmployeesModel;
public partial class _Default : System.Web.UI.Page
{
/**//// 编写日期 2009-2-21
/// 编写人sufei
/// <summary>
/// 前台界面实现对表的增删改查操作
/// </summary>
//产生一个类的对象,用于完成对sufei表的管理
sufeiServers objManageServers = new sufeiServers();
//定义一人静态的表用于存放数据
private static DataTable dt_Sufei = new DataTable();
//用于刷新数据的方法
private void BinderDataGriedView()
{
try
{
dt_Sufei = objManageServers.GetAllSufei(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[0];
AspNetPager1.RecordCount = Convert.ToInt32(objManageServers.GetAllSufei(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[1].Rows[0][0].ToString().Trim());
//把数据库中的数据英文字段中文化
dt_Sufei.Columns[0].ColumnName = "用户名";
dt_Sufei.Columns[1].ColumnName = "密码";
dt_Sufei.Columns[2].ColumnName = "性别";
dt_Sufei.Columns[3].ColumnName = "年龄";
gvSufei.DataSource = dt_Sufei;
gvSufei.DataBind();
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message.ToString().Trim();
}
}
//加载时执行
protected void Page_Load(object sender, EventArgs e)
{
try
{
//非首次加载不执行
if (!IsPostBack)
{
BinderDataGriedView();
}
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message.ToString().Trim();
}
}
//删除一个数据
protected void gvSufei_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
//调用ManageServers类的DeleteOneManager方法用于删除一个数据
//产生一个实体的对象
sufei objsufei = new sufei();
objsufei.UserName = gvSufei.Rows[e.RowIndex].Cells[2].Text.ToString().Trim();
if (objManageServers.DeleteOneSufei(objsufei))
{
lblMessage.Visible = true;
lblMessage.Text = "删除成功";
}
else
{
lblMessage.Visible = true;
lblMessage.Text = "删除失败";
}
//刷新数据
BinderDataGriedView();
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message.ToString().Trim();
}
}
//添加
protected void btnEnter_Click(object sender, EventArgs e)
{
try
{
//产生一个实体的对角
sufei objsufei = new sufei();
objsufei .UserName=txtUserName.Text.ToString().Trim();
objsufei .Pwd =txtUserPwd.Text.ToString().Trim();
objsufei .Sex = txtSex.Text.ToString().Trim();
objsufei .Age = txtAge.Text.ToString().Trim();
//在这里是把上面的数据封装成一个对象
//调用ManageServers类的方法InsertSufei用于添加一个数据
if (objManageServers.InserSufei(objsufei))
{
lblMessage.Visible = true;
lblMessage.Text = "添加成功";
}
else
{
lblMessage.Visible = true;
lblMessage.Text = "添加失败";
}
//刷新数据
BinderDataGriedView();
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message.ToString().Trim();
}
}
//转到修改界面
protected void gvSufei_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("sufeiUpdate.aspx?username=" + gvSufei.SelectedRow.Cells[2].Text.ToString().Trim());
}
//分页时执行
protected void AspNetPager1_PageChanged1(object sender, EventArgs e)
{
BinderDataGriedView();
}
}
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EmployeesBLL;
using EmployeesModel;
public partial class _Default : System.Web.UI.Page
{
/**//// 编写日期 2009-2-21
/// 编写人sufei
/// <summary>
/// 前台界面实现对表的增删改查操作
/// </summary>
//产生一个类的对象,用于完成对sufei表的管理
sufeiServers objManageServers = new sufeiServers();
//定义一人静态的表用于存放数据
private static DataTable dt_Sufei = new DataTable();
//用于刷新数据的方法
private void BinderDataGriedView()
{
try
{
dt_Sufei = objManageServers.GetAllSufei(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[0];
AspNetPager1.RecordCount = Convert.ToInt32(objManageServers.GetAllSufei(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize)[1].Rows[0][0].ToString().Trim());
//把数据库中的数据英文字段中文化
dt_Sufei.Columns[0].ColumnName = "用户名";
dt_Sufei.Columns[1].ColumnName = "密码";
dt_Sufei.Columns[2].ColumnName = "性别";
dt_Sufei.Columns[3].ColumnName = "年龄";
gvSufei.DataSource = dt_Sufei;
gvSufei.DataBind();
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message.ToString().Trim();
}
}
//加载时执行
protected void Page_Load(object sender, EventArgs e)
{
try
{
//非首次加载不执行
if (!IsPostBack)
{
BinderDataGriedView();
}
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message.ToString().Trim();
}
}
//删除一个数据
protected void gvSufei_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
//调用ManageServers类的DeleteOneManager方法用于删除一个数据
//产生一个实体的对象
sufei objsufei = new sufei();
objsufei.UserName = gvSufei.Rows[e.RowIndex].Cells[2].Text.ToString().Trim();
if (objManageServers.DeleteOneSufei(objsufei))
{
lblMessage.Visible = true;
lblMessage.Text = "删除成功";
}
else
{
lblMessage.Visible = true;
lblMessage.Text = "删除失败";
}
//刷新数据
BinderDataGriedView();
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message.ToString().Trim();
}
}
//添加
protected void btnEnter_Click(object sender, EventArgs e)
{
try
{
//产生一个实体的对角
sufei objsufei = new sufei();
objsufei .UserName=txtUserName.Text.ToString().Trim();
objsufei .Pwd =txtUserPwd.Text.ToString().Trim();
objsufei .Sex = txtSex.Text.ToString().Trim();
objsufei .Age = txtAge.Text.ToString().Trim();
//在这里是把上面的数据封装成一个对象
//调用ManageServers类的方法InsertSufei用于添加一个数据
if (objManageServers.InserSufei(objsufei))
{
lblMessage.Visible = true;
lblMessage.Text = "添加成功";
}
else
{
lblMessage.Visible = true;
lblMessage.Text = "添加失败";
}
//刷新数据
BinderDataGriedView();
}
catch (Exception ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message.ToString().Trim();
}
}
//转到修改界面
protected void gvSufei_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("sufeiUpdate.aspx?username=" + gvSufei.SelectedRow.Cells[2].Text.ToString().Trim());
}
//分页时执行
protected void AspNetPager1_PageChanged1(object sender, EventArgs e)
{
BinderDataGriedView();
}
}
代码就开放过么多,大家有什么不明白的就留言问我,也可以给我要源代码呵呵