快速实现dNet三层架构项目图解
開發工具使用SharpDevelop,示例數據庫使用微軟提供的Northwind.mdb英文版;搭建dNet三層架構項目;
新建UI層;這層可以是web,也可以是form;這里使用form;
新建三個項目,bll層、dal層、model層,這三個是類庫項目,其生成的是DLL;?
完成以后如下;?
看一下dal層,是類庫項目,輸出為acdal.dll;此工具界面比VS簡陋;屬輕便型工具;?不方便安裝VS的情況下臨時用一下還是可以;
示例數據庫; 這個是訂單、顧客、商家、產品、供應商的數據庫;
先來用三層架構操作一張字段少些的表;其他表類似;?
添加引用;此工具沒有VS強;程序集需要從GAC添加,GAC大體是一個dnet程序集的緩存;如果部署項目到用戶電腦可能會碰到相關情況;?
手動添加System.Data;VS不需此步;添加項目間的引用,引用關系如下,
? ? DAL引用Model
? ? BLL引用DAL和Model
? ? UI層引用BLL和Model
這個不能錯,否則亂了;項目跑起來后每次構建解決方案,會生成各個DLL,并把生成dll拷貝到引用它的項目的文件夾下;
?添加之后如下,這里acessdemo這個是UI層;
方便起見;DAL層直接使用網上有的Access幫助類;UI層設計一個簡單界面;BLL層先做一個獲取表的全部信息函數;添加實體類,Shipper;然后跑一下;
出現OLEDB未注冊提示;此工具的項目屬性設置不熟悉,先大體看下;?
經過一些設置,還是OLEDB未注冊;然后看Access幫助類;
如下;
//conn_str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Environment.CurrentDirectory + "\\yxdain.accdb'";
????????????//conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Environment.CurrentDirectory + "\\Northwind_en.mdb'";
????????????conn_str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Environment.CurrentDirectory + "\\Northwind_en.mdb'";
使用OLEDB.4.0,就好了;
跑起來,可以查詢數據;這樣三層就走通了;UI調用BLL,BLL調用DAL,這中間要用到Model; Model就是跟表字段對應的一個類;VS可以根據表自動生成;? ? ? ? ? ?
?
然后再做一個添加函數;效果如下;
access 的插入sql如下格式,ID是自增字段;
insert into Shippers (CompanyName, Phone) values ("AAA company","12345");?
更新和刪除有時間再寫;
代碼;
UI;
/** Created by SharpDevelop.* User: Administrator* Date: 2020/11/1/周日* Time: 9:49* * To change this template use Tools | Options | Coding | Edit Standard Headers.*/ using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Data; using acbll; using acmodel;namespace acessdemo {/// <summary>/// Description of MainForm./// </summary>public partial class MainForm : Form{acbll.shipbll ship=new acbll.shipbll();DataTable dt = new DataTable();public MainForm(){//// The InitializeComponent() call is required for Windows Forms designer support.//InitializeComponent();//// TODO: Add constructor code after the InitializeComponent() call.//}void Button4Click(object sender, EventArgs e) //查看{dt=ship.Getshippers();dataGrid1.DataSource=dt;}void Button1Click(object sender, EventArgs e){acmodel.Shipper shiper1 = new acmodel.Shipper();shiper1.Companyname=textBox1.Text;shiper1.Phone=textBox2.Text; ship.Addshipper(shiper1);}} }BLL;
/** Created by SharpDevelop.* User: Administrator* Date: 2020/11/1/周日* Time: 10:03* * To change this template use Tools | Options | Coding | Edit Standard Headers.*/ using System; using System.Collections.Generic; using System.Data; using acdal; using acmodel;namespace acbll {/// <summary>/// Description of MyClass./// </summary>public class shipbll{private AccessHelper achelp=new AccessHelper();public DataTable Getshippers() //返回所有商家信息列表{string sql1 = "select * from Shippers";DataTable dt = new DataTable();dt = achelp.GetDataTableFromDB(sql1);return dt;}public int Addshipper(Shipper shipper) //新增商家信息{string sql="insert into Shippers (CompanyName, Phone) values (" + "'"+shipper.Companyname+"'"+","+ "'"+shipper.Phone+"'" + ");";int ret = achelp.ExcuteSql(sql);return ret;}public bool Changeshipper(Shipper shipper) //更新商家信息{return true;}public void Removeshipper(int shipperId) //移除商家信息{}} }Model;
/** Created by SharpDevelop.* User: Administrator* Date: 2020/11/1/周日* Time: 10:03* * To change this template use Tools | Options | Coding | Edit Standard Headers.*/ using System; using System.Collections.Generic;namespace acmodel {/// <summary>/// Description of MyClass./// </summary>public class Shipper{protected int shipid;protected string companyname = String.Empty;protected string phone = String.Empty;public Shipper(){}public int Shipid{get { return shipid; }set { shipid = value; }}public string Companyname{get { return companyname; }set { companyname = value; }}public string Phone{get { return phone; }set { phone = value; }}} }DAL層是Access幫助類;
Access幫助類見此;
https://blog.csdn.net/bcbobo21cn/article/details/52201955
總結
以上是生活随笔為你收集整理的快速实现dNet三层架构项目图解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#特性 - 译
- 下一篇: winform 界面库SunnyUI初次