项目中我对"委托"的应用

第一步  定义委托:(基类)

          public delegate object PopulateDelegate(IDataReader dr);

第二步 符合委托定义的方法:(基类)
protected object Populatesys_User(IDataReader dr)
{
   sys_UserTable nc = new sys_UserTable();//sys_UserTable用户实体类

   if (!Convert.IsDBNull(dr["UserID"])) nc.UserID = Convert.ToInt32(dr["UserID"]); // 用户ID号
   if (!Convert.IsDBNull(dr["U_LoginName"])) nc.U_LoginName = Convert.ToString(dr["U_LoginName"]).Trim(); // 登陆名
   if (!Convert.IsDBNull(dr["U_Password"])) nc.U_Password = Convert.ToString(dr["U_Password"]).Trim(); // 密码md5加密字符
   if (!Convert.IsDBNull(dr["U_CName"])) nc.U_CName = Convert.ToString(dr["U_CName"]).Trim(); // 中文姓名
   if (!Convert.IsDBNull(dr["U_EName"])) nc.U_EName = Convert.ToString(dr["U_EName"]).Trim(); // 英文名
   return nc;
 }


第三步  实例化委托(继承类)
public ArrayList sys_UserList(QueryParam qp, out int RecordCount)
{
   PopulateDelegate mypd = new PopulateDelegate(Populatesys_User);//实例化委托
   return this.GetObjectList(mypd, qp, out RecordCount);
}

 

//公共类
private ArrayList GetObjectList(PopulateDelegate pd, QueryParam pp, out int RecordCount)
 {
       ArrayList lst = new ArrayList();
       RecordCount = 0;
       using (SqlConnection conn = GetSqlConnection())
            {
                SqlCommand cmd = new SqlCommand("SupesoftPage", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                // 设置参数
                cmd.Parameters.Add("@TableName", SqlDbType.NVarChar, 500).Value = pp.TableName;
                cmd.Parameters.Add("@ReturnFields", SqlDbType.NVarChar, 500).Value = pp.ReturnFields;
                cmd.Parameters.Add("@Where", SqlDbType.NVarChar, 500).Value = pp.Where;
                cmd.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pp.PageIndex;
                cmd.Parameters.Add("@PageSize", SqlDbType.Int).Value = pp.PageSize;
                cmd.Parameters.Add("@Orderfld", SqlDbType.NVarChar, 200).Value = pp.Orderfld;
                cmd.Parameters.Add("@OrderType", SqlDbType.Int).Value = pp.OrderType;
                // 执行
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    lst.Add(pd(dr));
                }
                // 取记录总数 及页数
                if (dr.NextResult())
                {
                    if (dr.Read())
                    {
                        RecordCount = Convert.ToInt32(dr["RecordCount"]);
                    }
                }

                dr.Close();
                cmd.Dispose();
                conn.Close();
            }
            return lst;
 }

你可能感兴趣的:(项目)