数据源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
*/
}
}

时间: 2024-11-02 01:46:00

数据源ObjectDataSource的数据访问类的编写的相关文章

Access 通用数据访问类(asp.net 2.0 c#)

access|asp.net|访问|数据 仿照以前收集的一个经典sql server数据访问类,稍做修改.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;usi

我的数据访问类(第二版)—— for .net2.0 (一)

asp.net2.0已经出来好久了,由于许多的原因一直没有使用,一个月前才开始使用VS2005写东西. 这一个月里又重新学习了一下基础知识,比如多态.接口了什么的. 既然已经到了2.0了嘛,那么以前的数据访问的方式要不要变一下呢?简单看了一下,感觉还是我的那种方式好,至少时我已经用习惯了.那么用.net2.0的方法重写一遍吧. 看了一下Framework 2.0的代码,发现一个问题.虽然表面上ADO.NET的使用没有什么变化(加了一些功能,原来由的功能没有变),但是内部实现有比较大的变化,原来的

我的数据访问类(第二版)—— for .net2.0 (二)

下面写一下相对来说不变的地方 SQL语句部分,改成了静态函数的形式.   1using System;  2using System.Collections.Generic;  3using System.Text;  4using System.Data;  5using System.Data.Common;  6using System.Data.SqlClient;  7using JYK;  8  9namespace JYK.DataAccessLibrary 10{ 11    /

中小系统.net DataAccess数据访问类

access|访问|数据   #region 引用对象 using System;using System.Xml ;using System.Data;using System.Data.SqlClient;using System.Web; #endregionnamespace SysClassLibrary{ /// <summary> /// DataAccess 的摘要说明. /// <author>wuchen</author> /// <date&

ASP.NET 数据访问类

asp.net|访问|数据 using System;using System.Data;using System.Data.SqlClient; namespace SysClassLibrary{ /// <summary> /// DataAccess 的摘要说明. /// <description>数据处理基类,调用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,ref Da

C#数据访问类

访问|数据 public class operateDB { private static string conStr; public static OleDbConnection cnn; OleDbDataAdapter da; OleDbCommandBuilder cb; OleDbCommand cmd; //构造函数 #region initialize public operateDB() { // // TODO: 在此处添加构造函数逻辑 // cnn=new OleDbConn

数据库访问类的编写

数据库连接类的使用: 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; usi

OledbHelper ,c#访问OLEDB数据源的一个数据访问层公共接口文件

问题描述 usingSystem;usingSystem.Collections.Generic;usingSystem.Text;//引入必须的命名空间usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Configuration;namespaceDAL{publicclassSqlHelper{#region静态似有变量///<summary>///SqlConnection对象实例///</summary>priv

ASP.NET 数据访问类_实用技巧

using System; using System.Data; using System.Data.SqlClient;  namespace SysClassLibrary { /// <summary> /// DataAccess 的摘要说明. /// <description>数据处理基类,调用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,ref DataSet ds);