开发工具使用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
{
///
/// Description of MainForm.
///
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
{
///
/// Description of MyClass.
///
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
{
///
/// Description of MyClass.
///
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