深入.NET平台的软件系统分层开发

第一章 软件系统的分层开发

案例

学生管理

建类库DAL

添加类

建窗体把数据绑定到datagrivel

因为不同一个项目下

所以要引用

 数据访问层

Data Access Layer(DAL)

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

namespace MySchool.DAL
{
    //CRUD  Create Read Update Delete
    public class StudentDAL
    {
        //四个方法
        public void AddStudent()
        {

        }
        //读取所有的学生
        public DataTable SelectStudent()
        {
            string sql = "select *from student";
            string str="Data Source=.;initial catalog=MySchool; uid=Sa";
            SqlConnection con=new SqlConnection(str);
            SqlDataAdapter da=new SqlDataAdapter(sql,con);
            DataSet ds=new DataSet();
            da.Fill(ds,"stuInfo");
            return ds.Tables["stuInfo"];
        }
        public void UpdateStudent()
        {
        }
        public void DeleteStudent()
        {
        }
    }
}
using MySchool.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MySchool.UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //添加引用
            StudentDAL dal=new StudentDAL();
            DataTable dt=dal.SelectStudent();
            dataGridView1.DataSource = dt;
        }
    }
}

 常见问题

1.不能启动类库项目

2.项目下的一个类名,和真实的Class关键字名称不一致

三层代码掉用图

1.在一个解决方案下,挂在两个项目:.一个类库.窗体

2.在DAL层创建一个名称为StudentDAL的类,该类的结构如下:  并在该类中植入  一个共有的发光法 SelectStudent()  将来是要被UI层调用的  (内存中的一个集合)

 

 

 微软提供一个文件夹app,congig  XML文件用来储存数据库的链接命令

GAC(Global  Assembly  Cache)全局程序集缓存

 =====================================================================================================================

第二章实体

1.异常的分类:编译时异常 运行时异常

2.异常捕获日志

3.为了保证程序出现异常的情况下不中断执行

DivedByZeroException----ArthimaticException----SysyemException

Masage:对异常信息的一个描述

StackTrace:精准定位到异常的引发点,确切到行数

InnerException:SQl报错的情况下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            #region readyonly const(必考)
           //const 可以在类中和方法中使用 但是readonly自能在类中不能在方法中
            //编译.将源文件幻化成   中间文件的时候C#中的中间文件MSIL其实就是exe文件
            //静态方法中,只能直接访问静态成员
            //若想非静态,先new 再通过对象名,变量名访问
            //被const修饰的类成员默认加上了static关键字
            //readonly在运行还时赋值而const是在编译时系统就已经将值给了常亮
            //const只能修饰值类型和特殊的引用类型,string  readonly可以修饰所有数据类型
            #endregion
              
            }
        }
  }

 ========================================================================================

第三章从数据访问开始

using关键字可以引入命名空间

释放资源:释放资源的类型  是  非托管资源,不被clr(公共语言运行时)监管的资源  JIT托管

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

namespace CASE
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用using释放链接对象
            string str = "Data Source=.; Initial CataLog=Blog;Uid=Sa";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            con.Dispose();
            con.Open();
            Console.WriteLine("OK");
            Console.ReadKey();
            //1.导入命名空间
            //释放非托管资源
            //2.为什么出了{},资源就可以自动释放
            //因为出了{}的时候系统自动调用了对象的Dispose方法,Dispose(),内部调用了Close();
            //3/Dispose()和Close的区别
            //Dispose()销毁了和DB的COn
            //Close()没有销毁通道,还可以再次打开
            //4 using(对象){}
            //是否所有的对象都可以通过using释放
            //不是,对象不许有CLose()方法才能使用using
            //释放的对象必须实现IDisposable  接口
        }
    }
}

============================================================================== 

第四章业务的拓展三层构架

1.BLL

2.MD5加密

3.在C#中能开启事务

快捷键 Alter+Shift+主键

三层架构图

Three Layer Schema Diagram

事务的特性

原子性  一致性  隔离性 永久性 

begin  Transaction

commit Trabsaction

Rollback  Transaction

事务的锁机制

事务的并发访问策略

在SQL 中不加锁 回避事务

select *from grade with(nolock)

 如果ADO.Net中有实物的参与,真正能影响数据表记录的就不再是

你执行完玩Excecute系列方法之后,而是事务提交或者和i滚之后

让多个执行过程数据准确,保证多个执行单元的数据的完整性

事务队形的形成要在链接对象打开之后 Open

string str="";  /占用内存

string str=string.Empty;//占用内存。性能高于“”

string str=null;//不占内存

Mobel  和底层表对应的实体类

DAL  和数据表 相关的操作

BLL 隔离DAL和UI

UI 负责页面的回显

Common 通用曾

DataView 数据扩容

dv.RowFilter="studentname=xxx";

dv.Soet="studentno  desc";

 ====================================================================

第五章实体类Windows程序中的高级应用

1分层
DAL数据访问层  负责与数据库交互
Mobel 实体层   在三层中传递对象
Bll  业务逻辑层  引用DAL 负责业务逻辑处理
Common  工具类曾
2,各层之间的引用关系
BLL---->mobel和DAL
DAL --->Mobel
UI---->Mobel和BLLyijiCommon
3.多路异常捕获
try
{
}
cath (子类异常)
{
}
cath(父类异常){
}
4.异常架构图

SQLException
ArgumentNullException
FileNotFoundException
IOException
ApplicationException
5.常见的属性
Message:消息
StackTrace:堆栈消息更精准  行号
InnerException :SQL语句 

App.config
书写在UI层 但是制定的节点可以在DAl层2读取,因为编译后所有的依赖文件
都被放倒DeBug文件夹下和xxx.exe同一目录下
<connectionStrings>
  <add name="constr" connectionString=""/> 
<connectionStrings>
需要在DAL首先
   引入System.Configuration程序集
添加using引用using  Sysxxx
书写SQlHelper文件 
ConfigurationManager.connectionString["constr"].ConnectionString
6.const和readonly
   1.const修饰的数据类型只能是值类型和String
   2.const定义位置 const类和方法都行  readonly只能在类中
   3.const赋值时机:便宜是赋值,readonly运行时赋值

using 关键字
using可以引入命名空间
using也可以释放资源
using(IDisposible对象){
    释放的是非托管资源
}

close和Dispose()区别
连接对象被销毁 
7.参数化查询 
 万能登陆发
'or 1=1 --
SqlConnection
SqlCommnd
SqlDatReader
SqlDataAdapter
cmd.CommandTtype=CommandType.StoredProcedure
SqlParameter[] p=
{
  new SqlParameter("@gender",gender),
  new SqlParameter("@count",SqlDbType.Int)  output
  new SqlParameter("@name",SqlDbType.Int)    return

}
p[1].Direction=ParameterDirection.Output;
p[2].Direction=ParameterDirection.ReturnValue;
加入到cmd对象中
cmd.parameters.AddRange(p);
cmd.xxxxx();
p[1].Value
p[2].Value

8.加密
MD5CryptoProvider  m=new MD5CryptoProvider();
String str="xxxxx";
byte[] b1=Encoding.Default.GetByTes(str);
byte[] b2=m.ComputHash(bytes);
String b=String.Empty;
foreach(byte item in b2)
{
   b+=item.ToString("X2")
}

9.开启事务
Transcation tx=con.BeginTracrtion();
cnd.Transaction=tx;
tx.Commit();
10
select gradeid as 编号  ,gradename 年纪名称 from grade
原文地址:https://www.cnblogs.com/lcycn/p/7101585.html