三层

2022-08-18  本文已影响0人  山猪打不过家猪
三层结构
三层项目的创建
1. 添加一个空解决方案
image.png
2. 添加三层
image.png image.png
3. 创建web应用
image.png
image.png
4.Model层添加UserInfo类
namespace CZBK.TestProject.Model
{
    public class UserInfo
    {
        public int ID { get; set; }
        public string UserName { get; set; }
        public string UserPass { get; set; }
        public DateTime RegTime { get; set; }
        public string EmailAddr { get; set; }
    }
}

5.DAL层添加SqlHelper类

Web.config

    <connectionStrings>

        <add name="connStr" connectionString="server=127.0.0.1;uid=sa;pwd=shangxi;database=new_test" />
        
    </connectionStrings>
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CZBK.TestProject.DAL
{
    public class SqlHelper
    {
        private readonly static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        public static DataTable GetTable(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlDataAdapter atper = new SqlDataAdapter(sql, conn))
                {
                    atper.SelectCommand.CommandType = type;
                    if (pars!=null)
                    {
                        atper.SelectCommand.Parameters.AddRange(pars);
                    }
                    DataTable da = new DataTable();
                    atper.Fill(da);
                    return da;
                }
            }
        }

        public static int ExecuteNonQuery(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if ( pars !=null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
    }
}


上一篇下一篇

猜你喜欢

热点阅读