Daniel has skipped the role of beginners.
Take the Person.Address table of the database AdventureWorks as an example.
First, build a good framework
The PRJ expression layer, which uses WinForm.
The PRJBLL business logic layer, of course, is the class library
Prjdal The data access layer, of course, the class library.
Prjmodel model layer, of course, is a class library.
Second, expand
Three
The above structure is analyzed from bottom to top.
The Address.cs under Prjmodel corresponds to the table field one by one to be manipulated, and I'll just list a few fields here.
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 is very simple, there is only one DataGridView1 control, the interface is as follows:
The background code is as follows:
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 ();
}
}
}
Operating effect: