三层架构源代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Model; using BLL; namespace UI { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private UserService us = new UserService(); private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { User user = new User(); user.UserName = txtUserName.Text.Trim(); user.Password = txtPassword.Text.Trim(); if (us.CheckUser(user)) { MessageBox.Show("登录成功!"); //UI层 } else { MessageBox.Show("登录失败!"); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Model; using DAL; namespace BLL { public class UserService { private UserDAO dao = new UserDAO(); public bool CheckUser(User user) { User u = dao.GetUserByUserName(user.UserName); if (u == null) { return false; } else //bll层 { return user.Password == u.Password; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Model; using System.Data; using System.Data.SqlClient; namespace DAL { public class UserDAO { public User GetUserByUserName(string username) { string sql = "select * from users where username=@username"; List<SqlParameter> paras=new List<SqlParameter>(); paras.Add(new SqlParameter("@username",username)); DataTable dt= SqlHelper.ExecuteTable(sql,paras); User user = null; if (dt.Rows.Count == 1) { //dal层 user = new User(); DataRow row = dt.Rows[0]; user.UserId = Convert.ToInt32(row[0]); user.UserName = row[1].ToString(); user.Password = row[2].ToString(); } return user; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Model { public class User //实体层 { public int UserId { get; set; } public string UserName { get; set; } public string Password { get; set; } } } <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings> <add name="ConnStr" connectionString="server=.;database=schooldb;uid=sa;pwd=123"/> </connectionStrings> </configuration> 添加引用 程序集 configuration privarte static readonly string 名字=configrationmanager