数据源ObjectDataSource的数据访问类的编写

以下面这张表为例,编写ObjectDataSource的操作类文件;数据表结构如下:

数据源ObjectDataSource的数据访问类的编写

其基本思路是用User.cs类,包装数据表的各个字段,将User的字段都聚合在一个User对象,然后增、删、改。

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections.Generic; public class Users { private int _userid; private string _username; private string _userpwd; public int UserId { get { return this._userid; } set { this._userid = value; } } public string UserName { get { return this._username; } set { this._username = value; } } public string UserPwd { get { return this._userpwd; } set { this._userpwd = value; } } public Users() { } public Users(int UserId,string UserName,string UserPwd) { this._userid = UserId; this._username = UserName; this._userpwd = UserPwd; } //用SqlDataReader读出数据库数据来实例化User类。 public Users(SqlDataReader Dr) { this._userid = Convert.ToInt32(Dr["UserId"]); this._username = Dr["UserName"].ToString(); this._userpwd = Dr["UserPwd"].ToString(); } //添加用户; public static bool AddUser(Users singleUser) { bool Flage=false; string procname = "dbo.AddUser"; SqlParameter[] prams ={ new SqlParameter("@UserName",SqlDbType.VarChar,50), new SqlParameter("@UserPwd",SqlDbType.VarChar,100)}; prams[0].Value = singleUser.UserName; prams[1].Value = singleUser.UserPwd; int intResult = DataBase.RunExecute(procname, prams); if(intResult>0) { Flage = true; } return Flage; } //删除用户; public static bool DeleteUser(Users SingleUser) { bool Flage = false; string procname = "dbo.DeleteUser"; SqlParameter[] prams ={new SqlParameter("@UserId",SqlDbType.Int)}; prams[0].Value = SingleUser.UserId; int intResult = DataBase.RunExecute(procname, prams); if (intResult > 0) { Flage = true; } return Flage; /*对应存储过程 *ALTER PROCEDURE dbo.DeleteUser ( @UserId int ) AS delete from Users where UserId=@UserId RETURN */ } //更新用户; public static bool UpdateUser(Users singleUser) { bool Flage = false; string procname = "dbo.UpdateUser"; SqlParameter[] prams ={new SqlParameter("@UserId",SqlDbType.Int), new SqlParameter("@UserName",SqlDbType.VarChar,50), new SqlParameter("@UserPwd",SqlDbType.VarChar,100)}; prams[0].Value = singleUser.UserId; prams[1].Value = singleUser.UserName; prams[2].Value = singleUser.UserPwd; int intResult = DataBase.RunExecute(procname, prams); if (intResult > 0) { Flage = true; } return Flage; /*对应存储过程 * ALTER PROCEDURE dbo.UpdateUser ( @UserId int, @UserName varchar(50), @UserPwd varchar(100) ) AS update Users set UserName=@UserName,UserPwd=@UserPwd where UserId=@UserId RETURN */ } //更改密码; public static bool ChangePwd(int UserId,string UserPwd) { bool Flage = false; string procname = "dbo.ChangePwd"; SqlParameter[] prams ={new SqlParameter("@UserId",SqlDbType.Int), new SqlParameter("@UserPwd",SqlDbType.VarChar,100)}; prams[0].Value = UserId; prams[1].Value = UserPwd; int intResult = DataBase.RunExecute(procname, prams); if (intResult > 0) { Flage = true; } return Flage; /*对应存储过程 * ALTER PROCEDURE dbo.ChangePwd ( @UserId int, @UserPwd varchar(100) ) AS update Users set UserPwd=@UserPwd where UserId=@UserId RETURN */ } //验证用户; public static bool ValidateUser(string UserName,string UserPwd) { bool Flage = false; string procname = "dbo.ValidateUse"; SqlParameter[] prams ={new SqlParameter("@UserName",SqlDbType.VarChar,50), new SqlParameter("@UserPwd",SqlDbType.VarChar,100)}; prams[0].Value = UserName; prams[1].Value = UserPwd; int intResult = DataBase.RunExecuteScalar(procname, prams); if (intResult > 0) { Flage = true; } return Flage; /*ALTER PROCEDURE dbo.ValidateUse ( @UserName varchar(50), @UserPwd varchar(100) ) AS select count(*) from Users where UserName=@UserName and UserPwd=@UserPwd RETURN */ } //检验用户是否存在; public static bool IsValidateUserName(string UserName) { bool Flage = false; string procname = "dbo.IsValidateUserName"; SqlParameter[] prams ={new SqlParameter("@UserName",SqlDbType.VarChar,50)}; prams[0].Value = UserName; int intResult = DataBase.RunExecuteScalar(procname, prams); if (intResult > 0) { Flage = true; } return Flage; /*对应存储过程 * ALTER PROCEDURE dbo.IsValidateUserName ( @UserName varchar(50) ) AS select count(*) from Users where UserName=@UserName RETURN */ } //得到所有用户信息(用来绑定到ObjectDataSource的SelectMethod方法) public List<Users> GetAllUsers() { List<Users> AllUsers = new List<Users>(); string procname = "dbo.GetAllUsers"; SqlDataReader Dr = DataBase.RunProcGetReader(procname); while(Dr.Read()) { AllUsers.Add(new Users(Dr)); } Dr.Close(); return AllUsers; //对应存储过程: /* ALTER PROCEDURE dbo.GetAllUsers AS select * from Users RETURN*/ } public static string GetUserNameByUserId(int UserId) { string strUserName = string.Empty; string procname = "dbo.GetUserNameByUserId"; SqlParameter[] prams ={new SqlParameter("@UserId",SqlDbType.Int)}; prams[0].Value = UserId; SqlDataReader Dr = DataBase.RunProcGetReader(procname,prams); while (Dr.Read()) { strUserName = Dr["UserName"].ToString(); } Dr.Close(); return strUserName; /*对应存储过程 * ALTER PROCEDURE dbo.GetUserNameByUserId ( @UserId int ) AS select UserName from Users where UserId=@UserId RETURN*/ } public static int GetUserIdByUserName(string UserName) { int UserId = 0; string procname = "dbo.GetUserIdByUserName"; SqlParameter[] prams ={ new SqlParameter("@UserName", SqlDbType.VarChar,50) }; prams[0].Value = UserName; SqlDataReader Dr = DataBase.RunProcGetReader(procname,prams); while (Dr.Read()) { UserId = Convert.ToInt32(Dr["UserId"]); } Dr.Close(); return UserId; /*对应存储过程 * ALTER PROCEDURE dbo.GetUserIdByUserName ( @UserName varchar(50) ) AS select UserId from Users where UserName=@UserName RETURN */ } public static Users GetUserByUserId(int UserId) { Users SingleUser = new Users(); string procname = "dbo.GetUserByUserId"; SqlParameter[] prams ={ new SqlParameter("@UserId", SqlDbType.Int) }; prams[0].Value = UserId; SqlDataReader Dr = DataBase.RunProcGetReader(procname,prams); while (Dr.Read()) { SingleUser =new Users(Dr); } Dr.Close(); return SingleUser; /*对应存储过程 ALTER PROCEDURE dbo.GetUserByUserId ( @UserId int ) AS select * from Users where UserId=@UserId RETURN */ } }

上一篇:wordpress如何获取当前页面的URL


下一篇:Winform开发框架的业务对象统一调用方式