ASP.Net学习(三)

2020/7/17 7:37:13 人评论 次浏览 分类:学习教程

1.CRUD练习

(三层方法,与昨天不同)先看图

 

 

 

项目结构:

部分代码:

<%@ WebHandler Language="C#" Class="UserInfoList" %>

using System;
using _0715StudentProject.Model;
using _0715StudentProject.BLL;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

public class UserInfoList : IHttpHandler
{

    /// <summary>
    /// UserInfoList
    /// </summary>
    /// <param name="context"></param>
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/html";
        UserInfoService userInfoService = new UserInfoService();
        List<UserInfo> list = userInfoService.GetList();
        StringBuilder sb = new StringBuilder();
        foreach (UserInfo userInfo in list)
        {
            sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td><a href='DeleteUser.ashx?id={0}' class='deletes'>删除</a></td><td><a href='ShowDetail.ashx?uid={0}'>详细</a></td><td><a href='ShowEditUser.ashx?id={0}'>编辑</a></td></tr>",userInfo.Id,userInfo.UserName,userInfo.UserPass,userInfo.RegTime,userInfo.Email);
        }
        //读取模板文件
        string filePath = context.Request.MapPath("UserInfoList.html");
            string fileContent = File.ReadAllText(filePath);
            fileContent = fileContent.Replace("@tbody",sb.ToString());
            context.Response.Write(fileContent);

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

using System;
using _0715StudentProject.Model;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace _0715StudentProject.DAL
{
    public class UserInfoDal
    {
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> GetList()
        {
            string sql = "select * from Student";
            DataTable da = SqlHelper.GetDataTable(sql, CommandType.Text);
            List<UserInfo> list = null;
            if (da.Rows.Count > 0)
            {
                list = new List<UserInfo>();
                UserInfo user = null;
                foreach (DataRow row in da.Rows)
                {
                    user = new UserInfo();
                    LoadEnity(user, row);
                    list.Add(user);
                }
            }
            return list;

        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public int AddUserInfo(UserInfo userInfo)
        {
            string sql = "insert  into  Student (UserName,UserPwd,RegTime,Email)  values (@UserName,@UserPwd,@RegTime,@Email)";
            SqlParameter[] pars = {
                 new SqlParameter("@UserName",SqlDbType.NVarChar,32),
                 new SqlParameter("@UserPwd",SqlDbType.NVarChar,32),
                 new SqlParameter("@RegTime",SqlDbType.DateTime),
                 new SqlParameter("@Email",SqlDbType.NVarChar,32)
            };
            pars[0].Value = userInfo.UserName;
            pars[1].Value = userInfo.UserPass;
            pars[2].Value = userInfo.RegTime;
            pars[3].Value = userInfo.Email;
            return SqlHelper.ExecuteNonquery(sql, CommandType.Text, pars);




        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public int DeleteUserInfo(int UserId)
        {
            string sql = "delete from Student where UserId = @UserId";
            SqlParameter[] pars = { new SqlParameter("@UserId", SqlDbType.Int) };
            pars[0].Value = UserId;
            return SqlHelper.ExecuteNonquery(sql, CommandType.Text, pars);
        }

        /// <summary>
        /// 以用户ID查询用户信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public UserInfo GetUserInfo(int UserId)
        {
            string sql = "select * from Student where UserId =  @UserId";
            SqlParameter[] pars = { new SqlParameter("@UserId", SqlDbType.Int) };
            pars[0].Value = UserId;
            DataTable da = SqlHelper.GetDataTable(sql, CommandType.Text, pars);
            UserInfo userInfo = null;
            if (da.Rows.Count > 0)
            {
                userInfo = new UserInfo();
                LoadEnity(userInfo, da.Rows[0]);
            }
            return userInfo;
        }


        public int EditUserInfo(UserInfo userInfo)
        {
            string sql = "update Student set  UserName=@UserName,UserPwd=@UserPwd,RegTime=@RegTime,Email=@Email where UserId=@UserId";
            SqlParameter[] pars = {
                new SqlParameter("@UserName",SqlDbType.NVarChar,32),
                new SqlParameter("@UserPwd",SqlDbType.NVarChar,32),
                new SqlParameter("@RegTime",SqlDbType.DateTime),
                new SqlParameter("@Email",SqlDbType.NVarChar,32),
                new SqlParameter("@UserId",SqlDbType.Int),
            };
            pars[0].Value = userInfo.UserName;
            pars[1].Value = userInfo.UserPass;
            pars[2].Value = userInfo.RegTime;
            pars[3].Value = userInfo.Email;
            pars[4].Value = userInfo.Id;
            return SqlHelper.ExecuteNonquery(sql, CommandType.Text, pars);

        }





        private void LoadEnity(UserInfo user, DataRow row)
        {
            user.UserName = row["UserName"] != DBNull.Value ? row["UserName"].ToString() : string.Empty;
            user.UserPass = row["UserPwd"] != DBNull.Value ? row["UserPwd"].ToString() : string.Empty;
            user.Email = row["Email"] != DBNull.Value ? row["Email"].ToString() : string.Empty;
            user.Id = Convert.ToInt32(row["UserId"]);
            user.RegTime = Convert.ToDateTime(row["RegTime"]);
        }
    }
}

 

相关资讯

    暂无相关的资讯...

共有访客发表了评论 网友评论

验证码: 看不清楚?
    -->