大牛们略过,对初学者起抛砖引玉的作用。
以数据库AdventureWorks的Person.Address表为例。
一、建好框架
prj 表示层,这里用的是winForm.
prjBLL 业务逻辑层,当然是类库
PrjDAL 数据访问层,当然是类库啦
PrjModel 模型层,当然也是类库啦
二、展开
三、
以上结构中从下到上分析。
prjModel下的Address.cs 与要操作的表字段一一对应,我这里只列举几个字段。
Address.cs
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
namespace PrjModel
{
public class Address
{
private int addressID;
private string addressLine1;
private string city;
private string postalCode;
public int AddressID { get; set; }
public string
AddressLine1 { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
}
SqlHelper.cs
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Data;
using
System.Data.SqlClient;
namespace PrjDAL
{
public static class SqlHelper
{
static string conStr = "server=.;database=AdventureWorks;user
id=sa;password=sa;";
private static SqlConnection con;
public static SqlConnection Con
{
get
{
if (con == null)
{
con = new SqlConnection(conStr);
con.Open();
}
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (con.State == ConnectionState.Broken)
{
con.Close();
con.Open();
}
return con;
}
}
public static SqlDataReader GetReader(string sql)
{
SqlCommand cmd = Con.CreateCommand();
cmd.CommandText =
sql;
SqlDataReader sdr = cmd.ExecuteReader();
return sdr;
}
}
}
AddressDAL.cs
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Data;
using
System.Data.SqlClient;
using PrjModel;
namespace PrjDAL
{
public class AddressDAL
{
public List<Address> GetAllAddress()
{
string
sql = "select AddressID,addressline1,city,postalCode from person.address";
SqlDataReader sdr = SqlHelper.GetReader(sql);
List<Address> addresses = new List<Address>();
while
(sdr.Read())
{
Address address = new
Address();
address.AddressID = sdr.GetInt32(0);
address.AddressLine1 = sdr.GetString(1);
address.City =
sdr.GetString(2);
address.PostalCode = sdr.GetString(3);
addresses.Add(address);
}
return addresses;
}
}
}
AddressBLL.cs
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Data;
using
System.Data.SqlClient;
using PrjModel;
using PrjDAL;
namespace PrjBLL
{
public class AddressBLL
{
List<Address> address = null;
public List<Address>
GetAllAddress()
{
AddressDAL addressDAL = new
AddressDAL();
address = addressDAL.GetAllAddress();
return address;
}
}
}
Form1.cs 很简单,里面只放了一个dataGridView1控件,界面如下:
后台代码如下:
using System;
using System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using System.Drawing;
using
System.Linq;
using System.Text;
using System.Windows.Forms;
using
System.Data.SqlClient;
using PrjModel;
using PrjBLL;
namespace
Prj
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
AddressBLL addressBll = new AddressBLL();
private void
Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = addressBll.GetAllAddress();
}
}
}
运行效果:
老调重弹---C# winForm三层结构,布布扣,bubuko.com