基于DBDataAccess类的具体数据访问类,这些代码大部分都可以自动生成。

本文介绍了一个用户数据访问层的实现细节,包括读取、创建、更新和删除用户信息的方法。涉及条件查询、批量操作及关联表查询等功能,并展示了具体的SQL语句构造逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Data.SqlClient;
None.gif
None.gif
using SEM.DataEntity;
None.gif
using COM.Makinfo.DataAccess;
None.gif
namespace SEM.DataAccess.SysManage.RightManage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DASysUser 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DASysUser:DBAccess
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private DataTable dataSysUser;
InBlock.gif
InBlock.gif        
public DASysUser()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
read#region read
InBlock.gif    
InBlock.gif        
public DataTable ReadAllData(string strIsUsed,string strusername,string strusercode,string strareacode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();
InBlock.gif
InBlock.gif            
//Generate where condition string.
InBlock.gif
            string strWhereCo = "";
InBlock.gif            
InBlock.gif            
if(null != strIsUsed && strIsUsed != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWhereCo 
+= " and UserIsUse = '" + strIsUsed +"'";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(null != strusername && strusername != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWhereCo 
+= " and UserName like '%" + strusername +"%'";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(null != strusercode && strusercode != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWhereCo 
+= " and UserCode like '%" + strusercode.ToLower()  +"%'";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(null != strareacode && strareacode != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWhereCo 
+= " and AreaCode = '" + strareacode +"'";            
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif            
string sql    = " SELECT * from SysUser ";
InBlock.gif
InBlock.gif            
if(strWhereCo != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWhereCo 
= strWhereCo.Substring(4);
InBlock.gif                sql 
= sql + " Where " + strWhereCo;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,sql);
InBlock.gif
InBlock.gif            
return dataSysUser;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 通过部门编码获取所有的用户,注意结构 必须是,aa,aaa,这种数据结构才行,否则数据会出错。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="depcode"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif    
InBlock.gif        
//由于数据库设计的改变,这个方法有所改变,现在变成了从SysUserPost取数
InBlock.gif
         public DataTable GetAllUserByDepCode(string depcode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();
InBlock.gif
InBlock.gif            
string sql    = " SELECT * from SysUser  where  usercode in (select distinct usercode from SysUserDept where Depguid  = '" + depcode +"')";
InBlock.gif
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,sql);
InBlock.gif
InBlock.gif            
return dataSysUser;
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 读取人口职位关联表中的数据。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="postcode"></param>
InBlock.gif        
/// <param name="deptcode"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataTable ReadDataByUserPost(string strAreaCode,string strDepGuid,string  strPostGuid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();
InBlock.gif
InBlock.gif            
string sql    = " SELECT * from SysUser  where  usercode in "+
InBlock.gif                
"(select distinct usercode from SysUserPost "+
InBlock.gif                
" where areaCode='"+ strAreaCode+"' and DepGuid  = '" + strDepGuid +"' and postGuid='"  + strPostGuid + "')";
InBlock.gif
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,sql);
InBlock.gif
InBlock.gif            
return dataSysUser;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 读取人口角色关联表中的数据。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strAreaCode"></param>
InBlock.gif        
/// <param name="strRoleGuid"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataTable ReadDataByUserRole(string strAreaCode,string strRoleGuid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();
InBlock.gif
InBlock.gif            
string sql    = " SELECT * from SysUser  where  usercode in "+
InBlock.gif                
"(select distinct usercode from SysUserRole "+
InBlock.gif                
" where areaCode='"+ strAreaCode+"' and RoleGuid  = '" + strRoleGuid + "')";
InBlock.gif
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,sql);
InBlock.gif
InBlock.gif            
return dataSysUser;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="aryIDs"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataTable ReadDataByIDs(string[] aryIDs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();
InBlock.gif
InBlock.gif            
//Generate where condition string.
InBlock.gif
            string strWhereCo = "";
InBlock.gif            
foreach(string strValue in aryIDs)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWhereCo 
+= " or UserCode = '" + strValue.ToLower() +"'";
ExpandedSubBlockEnd.gif            }

InBlock.gif            strWhereCo 
= strWhereCo.Substring(4);
InBlock.gif        
InBlock.gif            
string sql    = " SELECT * from SysUser "+
InBlock.gif                
" Where " + strWhereCo;
InBlock.gif            
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,sql);
InBlock.gif
InBlock.gif            
return dataSysUser;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///  根据GUID(数组)查询人员信息(Daizh)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="aryGUIDs"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataTable ReadDataByGUIDs(string[] aryGUIDs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();
InBlock.gif
InBlock.gif            
//Generate where condition string.
InBlock.gif
            string strWhereCo = "";
InBlock.gif            
foreach(string strValue in aryGUIDs)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWhereCo 
+= " or UserGuid = '" + strValue.ToLower() +"'";
ExpandedSubBlockEnd.gif            }

InBlock.gif            strWhereCo 
= strWhereCo.Substring(4);
InBlock.gif        
InBlock.gif            
string sql    = " SELECT * from SysUser "+
InBlock.gif                
" Where " + strWhereCo;
InBlock.gif            
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,sql);
InBlock.gif
InBlock.gif            
return dataSysUser;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据GUID查询人员信息(Daizh)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="aryIDs"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataTable ReadDataByGuid(string strGuid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();    
InBlock.gif            
string sql    = "SELECT * from SysUser Where UserGuid = '"+ strGuid + "'";            
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,sql);
InBlock.gif            
return dataSysUser;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据Code查询人员信息(Daizh)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="aryIDs"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataTable ReadUserNameByCode(string strCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();    
InBlock.gif            
string strSQLCommand    = "SELECT * FROM SysUser Where UserCode = '" + strCode + "'";
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,strSQLCommand);
InBlock.gif            
return dataSysUser;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 得取地区数组和部门数组的所有用户  (Fenglx)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strDept">部门数组</param>
InBlock.gif        
/// <param name="strDept">部门数组</param>
ExpandedSubBlockEnd.gif        
/// <returns>用户</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public DataTable ReadDataArrayUserCode(string[] strArea,string[] strDept) dot.gif{
InBlock.gif            
this.dataSysUser = OsdSysUser.NewDataTable();
InBlock.gif
InBlock.gif            
string strAreaCode = "",strDeptCode = "";
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i=0 ; i < strArea.Length ; i++dot.gif{
InBlock.gif                strAreaCode 
+= ",'" + strArea[i] + "'";
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i=0 ; i < strDept.Length ; i++dot.gif{
InBlock.gif                strDeptCode 
+= ",'" + strDept[i] + "'";
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            strAreaCode 
= strAreaCode.Substring(1);
InBlock.gif            strDeptCode 
= strDeptCode.Substring(1);
InBlock.gif        
InBlock.gif
//            string sql = " SELECT DISTINCT SysUser.UserName, SysUser.UserGuid" +
InBlock.gif
//                         " FROM SysUserDept INNER JOIN SysUser ON SysUserDept.AreaCode = SysUser.AreaCode AND SysUserDept.UserCode = SysUser.UserCode" +
InBlock.gif
//                         " WHERE SysUserDept.AreaCode IN (" + strAreaCode + ") AND SysUserDept.DepGuid IN (" + strDeptCode + ")";
InBlock.gif
            string sql = " SELECT DISTINCT SysUser.UserName, SysUser.UserGuid" +
InBlock.gif                
" FROM SysUserDept INNER JOIN SysUser ON SysUserDept.UserCode = SysUser.UserCode" +
InBlock.gif                
" WHERE SysUserDept.AreaCode IN (" + strAreaCode + ") AND SysUserDept.DepGuid IN (" + strDeptCode + ")";
InBlock.gif            
InBlock.gif            
this.InitDataSetTableBySQL(ref dataSysUser,sql);
InBlock.gif
InBlock.gif            
return dataSysUser;    
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 read
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
create#region create
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Create  new income type info.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="defineData"></param>

InBlock.gif        public void Create(DataTable dataSysUser)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IDbCommand[] insert 
= this.GetInsertCommand(dataSysUser);
InBlock.gif            
InBlock.gif            
//Excute command list.
InBlock.gif
            this.ExcuteCmdList(insert);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
 create
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
update#region update
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Create income type info.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="dataSysUser"></param>

InBlock.gif        public void Update(DataTable dataSysUser)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] strUpdateColumns = OsdSysUser.Columns;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] strPKColumns = dot.gif{OsdSysUser.Col_UserGuid};
InBlock.gif        
InBlock.gif            IDbCommand[] update 
= this.GetUpdateCommand(dataSysUser,OsdSysUser.Columns,strPKColumns);
InBlock.gif
InBlock.gif            
//Excute command list.
InBlock.gif
            this.ExcuteCmdList(update);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 修改密码
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="dataSysUser"></param>

InBlock.gif        public void UpdatePass(DataTable dataSysUser)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] strUpdateColumns = dot.gif{OsdSysUser.Col_UserCode,OsdSysUser.Col_UserPass};
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] strPKColumns = dot.gif{OsdSysUser.Col_UserCode};
InBlock.gif        
InBlock.gif            IDbCommand[] update 
= this.GetUpdateCommand(dataSysUser,strUpdateColumns,strPKColumns);
InBlock.gif
InBlock.gif            
//Excute command list.
InBlock.gif
            this.ExcuteCmdList(update);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
 update
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
delete#region delete
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="aryIDs"></param>

InBlock.gif        public void DeleteDataByIDs(string[] aryIDs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IDbCommand delete 
= InstanceComm;
InBlock.gif            
//Generate where condition string.
InBlock.gif
            string strWhereCo = "";
InBlock.gif            
foreach(string strValue in aryIDs)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strWhereCo 
+= " or UserCode ='" + strValue.ToLower()  +"";
ExpandedSubBlockEnd.gif            }

InBlock.gif            strWhereCo 
= strWhereCo.Substring(4);
InBlock.gif            
InBlock.gif            
if(strWhereCo == "")
InBlock.gif                
return;
InBlock.gif
InBlock.gif            delete.CommandText 
= 
InBlock.gif                
" update SysUser set UserIsUse ='F' "+
InBlock.gif                
" Where " + strWhereCo;
InBlock.gif            
InBlock.gif            
//Excute command list.
ExpandedSubBlockStart.gifContractedSubBlock.gif
            this.ExcuteCmdList(new IDbCommand[]dot.gif{delete});
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
 delete
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/richardhu/archive/2006/07/25/459632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值