老調重彈---C# winForm三層結構

來源:互聯網
上載者:User

標籤:winform   datagridview   c   style   class   blog   

大牛們略過,對初學者起拋磚引玉的作用。

以資料庫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();
}

}
}

 

運行效果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.