ASP.NET MVC三层架构基础详细操作图文教程(VS2017)(1)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41715295/article/details/79383718

作者marker 欢迎转载!!!

参照原文地址:ASP.NET三层架构基础详细操作图文教程(一)作者:乔杨 出自:http://www.cnntec.com 作者:AZ猫 转发请注明出处,违者必究

本文主要讲述Asp.net MVC B/S结构 下基础的三层架构项目。
三层主要是指的界面UI层,逻辑层,数据层。
界面UI层:用于用户观看,体验的表示层。
逻辑层:程序运行逻辑的封装层。
数据层:程序数据相关操作的封装层。

每层当中还可以进行不同的详细划分,因为是基础教程,先领新手入门,所以不进行复杂的讲解。
准备工具:
Microsoft Visual Studio 2017以下简称vs2017
Microsoft SQLServer 2016 以下简称 数据库
安装就不详说了。
首先,我们新建一个项目:如下图

打开VS2017后,点击创建项目,选择C#中的web项目,选择ASP.NET web应用程序,填写名称为StudyCSharp.Web

选择保存的路径,填写解决方案名称StudyCSharp,记得勾下创建解决方杂的选项。确定建好项目后,选择MVC模版,


右侧的解决方案资源管理器如下图

 

现在我们要对项目进行三层架构,刚才我们在创建项目的时候,已经创建了UI层,即web项目。
现在我们要创建BLL层和DAL层等 。如下图

左侧,我们选择Visual C#项目,右则我们选择类库。
然后在名称中输入StudyCSharp.BLL
路径地址可以不用更改。

根据以上操作,我们再创建StudyCSharp.DAL和StudyCSharp.Entity及StudyCSharp.Utility
创建完如下图:

各项目说明:
StudyCSharp.Web项目:表示层,用于显示给用户观看和操作体验的
StudyCSharp.BLL项目:逻辑层,程序实现逻辑
StudyCSharp.DAL项目:数据层,程序数据相关操作封装
StudyCSharp.Entity项目:实体类,映射数据库表结构
StudyCSharp.Utility项目:实用应用层,封装相关应用操作

到此为止,三层的框架已经搭建好了,但是还不能正常运用,我们还要进行一些配置和修改。
首选,我们修改DAL层,删除默认的Class1.cs文件,新建SQLHelper.cs类和Configuration.cs类。
以下是Coniguration.cs类内容

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;

  5. namespace StudyCSharp.DAL
  6. {
  7.     public partial class Configuration
  8.     {
  9.         public static string DateFormat
  10.         {
  11.             get
  12.             {
  13.                 string formatStr = System.Configuration.ConfigurationManager.AppSettings["DateFormat"];
  14.                 if (formatStr == null)
  15.                 {
  16.                     return "yyyy/MM/dd HH:mm";
  17.                 }
  18.                 return "yyyy/MM/dd HH:mm";
  19.             }
  20.         }

  21.         private static string mConnectionString = string.Empty;
  22.         public static string ConnectionString
  23.         {
  24.             get
  25.             {
  26.                 if (mConnectionString == string.Empty)
  27.                 {
  28.                     mConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
  29.                 }

  30.                 return mConnectionString;
  31.             }
  32.             set
  33.             {
  34.                 mConnectionString = value;
  35.             }
  36.         }

  37.         static EnumNetDbType mDbType = EnumNetDbType.Sql;
  38.         public static EnumNetDbType DbType
  39.         {
  40.             get
  41.             {
  42.                 return mDbType;
  43.             }
  44.             set
  45.             {
  46.                 mDbType = value;
  47.             }
  48.         }

  49.         //public static int GetOracleSequenceNextValue(string sequenceName)
  50.         //{
  51.         //    string sequel = "select  " + sequenceName + ".nextval from dual";
  52.         //    return (int)(OracleHelper.ExecuteScalar(ConnectionString, CommandType.Text, sequel));
  53.         //}

  54.     }



  55.     public enum EnumNetDbType
  56.     {
  57.         Sql = 0,
  58.         Oracle = 1,
  59.         OleDb = 2
  60.     }
  61.     public enum EnumPersistStatus
  62.     {
  63.         New = 0,
  64.         Update = 1,
  65.         Delete = 2
  66.     }
  67. }

添加完2个类后,我们需对DAL项目的引用文件夹点右键,选择添加引用。如图

选择System.Configuration,确定,OK,完成以后我们再继续来配置web项目下的web.config文件,主要是连接数据库。
如下图

在appSettings节点下。原节点为<appSettings/>


OK,现在我们来配置实体类。
实体类为数据库表的映射,即与数据库的表对应。
我们建立数据库为StudyCSharp,建立表为UserInformation
具体建表一下会儿给出代码。
我们在Entity项目上点击右键,选择添加类,然后写入如下图数据。
与数据库中表UserInformation对应。如下:
public class UserInformation

{

     public int ID{get;set;}

     public string UserName{get;set;}

     public string UserPassword{get;set;}

}

们做了这个数据库的实体映射后我们要怎么用呢?怎么才能操作这个实体像在操作数据库一样的呢?
我们马上来配置DAL项目。
对DAL项目右键,选择新建类.因为是直接项目右键,所以不用做选择。直接输入名称即可
这里我们是操作的UserInformation所以我们类名命名为Userinformation_DAL.cs,确定
如下图:
然后在类中,我们添加如下名命空间的引用:

  1. Using System.Data;
  2. Using System.Data.SqlClient;

并将类前面加上public,继续添加引用

在弹出的对话框中,我们选择项目栏,选中刚才我们建立的项目的映射类
如下图


需要的数据库脚本如下。
创建表和相关存储过程。这个不是我们主讲的核心,所以略过,附上脚本。

  1. USE [StudyCSharp]
  2. GO

  3. SET ANSI_NULLS ON
  4. GO
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7. CREATE TABLE [dbo].[SC_Userinformation](
  8.     [ID] [int] IDENTITY(1,1) NOT NULL,
  9.     [UserName] [nvarchar](32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
  10.     [UserPassword] [nvarchar](128) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
  11. CONSTRAINT [PK_SC_Userinformation] PRIMARY KEY CLUSTERED
  12. (
  13.     [ID] ASC
  14. )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
  15. ) ON [PRIMARY]


  16. USE [StudyCSharp]
  17. GO

  18. SET ANSI_NULLS ON
  19. GO
  20. SET QUOTED_IDENTIFIER ON
  21. GO
  22. CREATE PROCEDURE [dbo].[SP_SelectAllFromUserinformation]        
  23. AS
  24. BEGIN
  25.         Select * from SC_userinformation
  26. END


  27. USE [StudyCSharp]
  28. GO

  29. SET ANSI_NULLS ON
  30. GO
  31. SET QUOTED_IDENTIFIER ON
  32. GO
  33. CREATE PROCEDURE [dbo].[SP_SelectAllFromUserinformationById]
  34.         @UID int        
  35. AS
  36. BEGIN
  37.         Select * from SC_userinformation where id=@UID
  38. END


  39. USE [StudyCSharp]
  40. GO

  41. SET ANSI_NULLS ON
  42. GO
  43. SET QUOTED_IDENTIFIER ON
  44. GO
  45. CREATE PROCEDURE [dbo].[SP_InsertUserinformation]
  46.         @ID int=null,
  47.         @UserName nvarchar(32),
  48.         @UserPassword nvarchar(128)
  49. AS
  50. BEGIN
  51.         Insert into SC_Userinformation
  52.         (
  53.                 UserName,
  54.                 UserPassword
  55.         )
  56.         values
  57.         (
  58.                 @UserName,
  59.                 @UserPassword
  60.         )
  61.         select @@identity as 'identity'
  62. END


  63. USE [StudyCSharp]
  64. GO

  65. SET ANSI_NULLS ON
  66. GO
  67. SET QUOTED_IDENTIFIER ON
  68. GO
  69. CREATE PROCEDURE [dbo].[SP_UpdateUserinformation]
  70.         @ID int,
  71.         @UserName nvarchar(32),
  72.         @UserPassword nvarchar(128)
  73. AS
  74. BEGIN
  75.         Update SC_Userinformation
  76.         SET UserName=@UserName,
  77.                 UserPassword=@UserPassword
  78.         where ID=@ID
  79. END


  80. USE [StudyCSharp]
  81. GO

  82. SET ANSI_NULLS ON
  83. GO
  84. SET QUOTED_IDENTIFIER ON
  85. GO
  86. CREATE PROCEDURE [dbo].[SP_UserLogin]
  87.         @UserName nvarchar(32),
  88.         @UserPassword nvarchar(128)
  89. AS
  90. BEGIN
  91.         select count(id)
  92.         from SC_UserInformation
  93.         where username=@UserName
  94.         and userPassword=@UserPassword
  95. END
复制代码

存储过程相关调用,详细Userinformation_DAL.cs中的说明。

附上Userinformation_DAL.cs文件内容如下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SqlClient;

  6. namespace StudyCSharp.DAL
  7. {
  8.     public partial class Userinformation_DAL
  9.     {
  10.         /// <summary>
  11.         /// 用户登录验证
  12.         /// </summary>
  13.         /// <param name="userName">用户名</param>
  14.         /// <param name="userPassword">密码</param>
  15.         /// <returns>布尔值True成功</returns>
  16.         public static bool UserLogin(string userName, string userPassword)
  17.         {
  18.             string sequel = "SP_UserLogin";
  19.             SqlParameter[] paras = new SqlParameter[]
  20.             {
  21.                 new SqlParameter("@UserName", userName),
  22.                 new SqlParameter("@UserPassword",userPassword)
  23.             };
  24.             int result =(int)SqlHelper.ExecuteScalar(Configuration.ConnectionString, CommandType.StoredProcedure, sequel, paras);
  25.             if (result > 0)
  26.             {
  27.                 return true;
  28.             }
  29.             else
  30.             {
  31.                 return false;
  32.             }
  33.         }

  34.         /// <summary>
  35.         /// 添加新用户
  36.         /// </summary>
  37.         /// <param name="ui">用户信息实体</param>
  38.         /// <returns>用户编号</returns>
  39.         public static int CreateUserInfo(StudyCSharp.Entity.UserInformation ui)
  40.         {
  41.             string sequel = "SP_InsertUserinformation";
  42.             SqlParameter[] paras = (SqlParameter[])ValueParas(ui);
  43.             int result = SqlHelper.ExecuteNonQuery(Configuration.ConnectionString, CommandType.StoredProcedure, sequel, paras);
  44.             return result;
  45.         }


  46.         /// <summary>
  47.         /// 获取用户所有信息
  48.         /// </summary>
  49.         /// <returns>泛型实体</returns>
  50.         public static List<StudyCSharp.Entity.UserInformation> GetAllUserInfo()
  51.         {
  52.             string sequel = "SP_SelectAllFromUserinformation";            
  53.             DataTable dt = SqlHelper.ExecuteDataSet(Configuration.ConnectionString, CommandType.StoredProcedure, sequel,null).Tables[0];
  54.             return LoadListFromDataView(dt.DefaultView);
  55.         }

  56.         /// <summary>
  57.         /// 获取用户所有信息同上,不同的是不是调用的存储过程,而是直接拼写的SQL
  58.         /// </summary>
  59.         /// <returns>DataTable</returns>
  60.         public static DataTable GetAllUserInfoBySql()
  61.         {
  62.             string sequel = "Select * from SC_UserInformation";
  63.             DataTable dt = SqlHelper.ExecuteDataSet(Configuration.ConnectionString, CommandType.Text, sequel, null).Tables[0];
  64.             return dt;
  65.         }



  66.         /// <summary>
  67.         /// 获取某用户信息
  68.         /// </summary>
  69.         /// <param name="empId">用户id</param>
  70.         /// <returns>用户信息实体</returns>
  71.         public static StudyCSharp.Entity.UserInformation GetUserInfoByEmpId(string empId)
  72.         {
  73.             string sequel = "SP_SelectAllFromUserinformationById";
  74.             SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@UID", empId) };
  75.             DataTable dt = SqlHelper.ExecuteDataSet(Configuration.ConnectionString, CommandType.StoredProcedure, sequel, paras).Tables[0];
  76.             if (dt.Rows.Count == 0)
  77.             {
  78.                 return null;
  79.             }
  80.             else
  81.             {
  82.                 DataRow row = dt.Rows[0];
  83.                 return GetEntity(row);
  84.             }
  85.         }

  86.         /// <summary>
  87.         /// 更新用户信息
  88.         /// </summary>
  89.         /// <param name="ui">用户实体</param>
  90.         /// <returns>影响行数</returns>
  91.         public static int UpdateUserInfo(StudyCSharp.Entity.UserInformation ui)
  92.         {
  93.             string sequel = "SP_UpdateUserinformation";
  94.             SqlParameter[] paras = (SqlParameter[])ValueParas(ui);
  95.             int result = SqlHelper.ExecuteNonQuery(Configuration.ConnectionString, CommandType.StoredProcedure, sequel, paras);
  96.             return result;  
  97.         }

  98.         /// <summary>
  99.         /// 将DataView转换为泛型实体对象
  100.         /// </summary>
  101.         /// <param name="dv">DataView</param>
  102.         /// <returns>泛型实体对象</returns>
  103.         private static List<StudyCSharp.Entity.UserInformation> LoadListFromDataView(DataView dv)
  104.         {
  105.             List<StudyCSharp.Entity.UserInformation> list = new List<StudyCSharp.Entity.UserInformation>();
  106.             for (int index = 0; index <= dv.Count - 1; index++)
  107.             {
  108.                 list.Add(GetEntity(dv[index].Row));
  109.             }
  110.             return list;
  111.         }

  112.                 /// <summary>
  113.                 /// 从DataReader中读取数据映射到实体类的属性中
  114.                 /// </summary>
  115.                 /// <remarks></remarks>
  116.                 private static StudyCSharp.Entity.UserInformation GetEntity(IDataReader dataReader)
  117.                 {
  118.                         StudyCSharp.Entity.UserInformation _obj = new StudyCSharp.Entity.UserInformation();
  119.                         if (!dataReader["ID"].Equals(DBNull.Value))
  120.                                 _obj.ID = Convert.ToInt32(dataReader["ID"]);
  121.                         if (!dataReader["UserName"].Equals(DBNull.Value))
  122.                                 _obj.UserName = Convert.ToString(dataReader["UserName"]);
  123.                         if (!dataReader["UserPassword"].Equals(DBNull.Value))
  124.                                 _obj.UserPassword = Convert.ToString(dataReader["UserPassword"]);
  125.                         return _obj;
  126.                 }
  127.                 /// <summary>
  128.                 /// 从行中读取数据映射到实体类的属性中
  129.                 /// </summary>
  130.                 /// <remarks></remarks>
  131.                 private static StudyCSharp.Entity.UserInformation GetEntity(DataRow row)
  132.                 {
  133.                         StudyCSharp.Entity.UserInformation _obj = new StudyCSharp.Entity.UserInformation();
  134.                         if (!row["ID"].Equals(DBNull.Value))
  135.                                 _obj.ID = Convert.ToInt32(row["ID"]);
  136.                         if (!row["UserName"].Equals(DBNull.Value))
  137.                                 _obj.UserName = Convert.ToString(row["UserName"]);
  138.                         if (!row["UserPassword"].Equals(DBNull.Value))
  139.                                 _obj.UserPassword = Convert.ToString(row["UserPassword"]);
  140.                         return _obj;
  141.                 }

  142.                 /// <summary>
  143.                 /// 该数据访问对象的属性值装载到数据库更新参数数组Insert用
  144.                 /// </summary>
  145.                 /// <remarks></remarks>
  146.                 private static IDbDataParameter[] ValueParas(StudyCSharp.Entity.UserInformation _obj)
  147.                 {
  148.                         SqlParameter[] paras = new SqlParameter[3];
  149.                         paras[0] = new SqlParameter("@ID", _obj.ID);
  150.                         paras[1] = new SqlParameter("@UserName", _obj.UserName);
  151.                         paras[2] = new SqlParameter("@UserPassword", _obj.UserPassword);

  152.                         paras[0].DbType = DbType.Int32;
  153.                         paras[1].DbType = DbType.String;
  154.                         paras[2].DbType = DbType.String;

  155.                         return paras;
  156.                 }
  157.         }
  158. }

下篇解析三层架构的DAL及BLL还有WEB之间的关系和调用。


展开阅读全文

没有更多推荐了,返回首页