C# Linq版七层登录

不明白七层的,可以看下我的上一篇文章,内容与直接连接SQL的没有太大区别,在Entity中少了一个转换类,DAL中多了一个Linqtosql类,….

将数据库文件添加到vs中
具体步骤
视图->服务器资源管理器
效果图
这里写图片描述
右键数据连接->添加连接 ->数据源(我选的是数据库文件)->数据库文件名 (点击浏览找到你自己的数据库)->确定
这里写图片描述

在DAL中要添加 我的是LinqToSQL.dbml
这里写图片描述

将你连接在vs上的数据库中的表拖进去
效果图
这里写图片描述

总构图
这里写图片描述

Entity层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Entity
{
    public class UserInfo
    {
        private string userName;
        private string password;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }
}

IDAL层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IDAL
{
    public interface IUserLoginDAL
    {
        List<Entity.UserInfo> Login(Entity.UserInfo userInfo);
    }
}

DAL
UserLoginDAL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class UserLoginDAL:IDAL.IUserLoginDAL
    {
        public List<Entity.UserInfo> Login(Entity.UserInfo userInfo)
       {
         List<Entity.UserInfo> user = new List<Entity.UserInfo>();
         LinqToSqlDAL login = new LinqToSqlDAL();
         try
         {
             var query = login.LoginLinqToSql(userInfo);
             user.Add(new Entity.UserInfo { UserName = query[0].UserName, Password = query[0].Password });
         }
         catch
         {
             user = null;
             return user;
         }
             return user;
       }
    }
}

LinqToSqlDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;

namespace DAL
{
    class LinqToSqlDAL
    {
        public List<User_Info> LoginLinqToSql(Entity.UserInfo userInfo)
        {
            string Constr = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
            LinqToSQLDataContext db = new LinqToSQLDataContext(Constr);
            Table<User_Info> tbl = db.GetTable<User_Info>();
            var query1 = db.User_Info.Where(s => s.UserName.Equals(userInfo.UserName) && s.Password.Equals(userInfo.Password)).ToList();
            return query1;

        }
    }
}

IBLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IBLL
{
    public interface IUserLoginBLL
    {
        bool Login(Entity.UserInfo userInfo);
    }
}

BLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
namespace BLL
{
    public class UserLoginBLL:IBLL.IUserLoginBLL
    {
        public bool Login(Entity.UserInfo userInfo)
        { 
            Factory.FactoryDAL fact = new Factory.FactoryDAL();
            IDAL.IUserLoginDAL idal = fact.CreatDAL();
            List<Entity.UserInfo> userList = idal.Login(userInfo);
            bool flag;
            if (userList!=null)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            return flag;
        }
    }
}

Factory
FactoryBLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
namespace Factory
{
   public class FactoryBLL
    {
       string AssemblyName = "BLL";
       string StrDAL = System.Configuration.ConfigurationManager.AppSettings["BLL"];

       public IBLL.IUserLoginBLL CreatBLL()
       {
           string className = AssemblyName + ".UserLoginBLL";
           IBLL.IUserLoginBLL ibll = (IBLL.IUserLoginBLL)Assembly.Load(StrDAL).CreateInstance(className);
           return ibll;
       }
    }
}

FactoryDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace Factory
{
    public class FactoryDAL
    {
        string AssemblyName = "DAL";
        string StrDAL = System.Configuration.ConfigurationManager.AppSettings["DAL"];

        public IDAL.IUserLoginDAL CreatDAL()
        {
            string className = AssemblyName + ".UserLoginDAL";
            IDAL.IUserLoginDAL idal = (IDAL.IUserLoginDAL)Assembly.Load(StrDAL).CreateInstance(className);
            return idal;
        }
    }
}

UI
这里写图片描述

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;

namespace UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "")
            {
                MessageBox.Show("用户名不能为空");
            }
            if (txtPassword.Text == "")
            {
                MessageBox.Show("密码不能为空");
            }
            try
            {
                Entity.UserInfo user = new Entity.UserInfo();
                user.UserName = txtUserName.Text.Trim();
                user.Password = txtPassword.Text;
                Factory.FactoryBLL bllfact = new Factory.FactoryBLL();
                IBLL.IUserLoginBLL ibll = bllfact.CreatBLL();
                if (ibll.Login(user))
                {
                    txtUserName.Clear();
                    txtPassword.Clear();
                    MessageBox.Show("登录成功");
                }
                else
                {

                    txtUserName.Clear();
                    txtPassword.Clear();
                    MessageBox.Show("登录失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ConnStr" value="Server=.;User=sa;Pwd=12345;Database=UserLogin"/>
    <add key="DAL" value="DAL"/>
    <add key="BLL" value="BLL"/>
  </appSettings>
</configuration>

如有不对,欢迎提出,及时修改。

版权声明:本文为博主原创文章,转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值