標籤:cache 之間 orm 語言 one turn ret app 自動
第一章 軟體系統的分層開發
案例
學生管理
建類庫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和DALDAL --->MobelUI---->Mobel和BLLyijiCommon3.多路異常捕獲try{}cath (子類異常){}cath(父類異常){}4.異常架構圖SQLExceptionArgumentNullExceptionFileNotFoundExceptionIOExceptionApplicationException5.常見的屬性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"].ConnectionString6.const和readonly 1.const修飾的資料類型只能是實值型別和String 2.const定義位置 const類和方法都行 readonly只能在類中 3.const賦值時機:便宜是賦值,readonly運行時賦值using 關鍵字using可以引入命名空間using也可以釋放資源using(IDisposible對象){ 釋放的是非託管資源}close和Dispose()區別連線物件被銷毀 7.參數化查詢 萬能登陸發‘or 1=1 --SqlConnectionSqlCommndSqlDatReaderSqlDataAdaptercmd.CommandTtype=CommandType.StoredProcedureSqlParameter[] 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].Valuep[2].Value8.加密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();10select gradeid as 編號 ,gradename 年紀名稱 from grade
深入.NET平台的軟體系統分層開發