標籤: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();
}
}
}
運行效果: