老调重弹---C# winForm三层结构
2020-12-13 02:33
标签:winform datagridview c style class blog 大牛们略过,对初学者起抛砖引玉的作用。 以数据库AdventureWorks的Person.Address表为例。 一、建好框架 prj 表示层,这里用的是winForm. prjBLL 业务逻辑层,当然是类库 PrjDAL 数据访问层,当然是类库啦 PrjModel 模型层,当然也是类库啦 二、展开 三、 以上结构中从下到上分析。 prjModel下的Address.cs 与要操作的表字段一一对应,我这里只列举几个字段。 Address.cs using System; namespace PrjModel public int AddressID { get; set; } } SqlHelper.cs using System; namespace PrjDAL public static SqlDataReader GetReader(string sql) } AddressDAL.cs using System; namespace PrjDAL AddressBLL.cs using System; namespace PrjBLL Form1.cs 很简单,里面只放了一个dataGridView1控件,界面如下: 后台代码如下: using System; } 运行效果: 老调重弹---C# winForm三层结构,搜素材,soscw.com 老调重弹---C# winForm三层结构 标签:winform datagridview c style class blog 原文地址:http://www.cnblogs.com/dychimaojin/p/3773884.html
using System.Collections.Generic;
using
System.Linq;
using System.Text;
{
public class Address
{
private int addressID;
private string addressLine1;
private string city;
private string postalCode;
public string
AddressLine1 { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Data;
using
System.Data.SqlClient;
{
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;
}
}
{
SqlCommand cmd = Con.CreateCommand();
cmd.CommandText =
sql;
SqlDataReader sdr = cmd.ExecuteReader();
return sdr;
}
}
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Data;
using
System.Data.SqlClient;
using PrjModel;
{
public class AddressDAL
{
public List GetAllAddress()
{
string
sql = "select AddressID,addressline1,city,postalCode from person.address";
SqlDataReader sdr = SqlHelper.GetReader(sql);
List addresses = new List();
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;
}
}
}
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Data;
using
System.Data.SqlClient;
using PrjModel;
using PrjDAL;
{
public class AddressBLL
{
List address = null;
public List
GetAllAddress()
{
AddressDAL addressDAL = new
AddressDAL();
address = addressDAL.GetAllAddress();
return address;
}
}
}
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();
}
}
上一篇:一致性hash算法