当前位置: 首页 > news >正文

练习:WinForm 登录界面

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

namespace 登陆界面.DataConnection
{
    class DataConnection
    {
        private static string connstr = "server=.; database=mydb; user=sa; pwd=ray; ";
        public static SqlConnection Conn
        {
            get 
            {                           
                return new SqlConnection (connstr);
            }
        }
    }
}
数据连接类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 登陆界面.Model
{
    class LoginTable
    {
        private string username;

        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }
}
实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace 登陆界面.DataOperation
{
    class LoginData
    {
        private SqlConnection _conn;
        private SqlCommand _cmd;
        private SqlDataReader _dr;

        public LoginData()
        {
            _conn = DataConnection.DataConnection.Conn;
            _cmd = _conn.CreateCommand();
        }

        //通过用户名验证密码是否正确
        public bool Checked(string name,string pwd)
        {
            _cmd.CommandText = "select Password from Login where UserName=@name";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@name",name);
            _conn.Open();
            _dr = _cmd.ExecuteReader();
            if (_dr.HasRows)
            {
                _dr.Read();
                if (_dr[0].ToString() == pwd)
                {
                    return true;
                }
                else
                { return false; }
            }
            else
            { return false; }
            _conn.Close();
        }
    }
}
数据操作类
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 登陆界面
{
    public partial class Login : Form
    {   
        //定义成员变量
        public string username;

        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            //取出用户名和密码
            string uid = txtUID.Text;
            string pwd = txtPWD.Text;

            if (uid != "" && pwd != "")
            {
                DataOperation.LoginData data = new DataOperation.LoginData();
                if (data.Checked(uid, pwd))
                {
                    username=uid; //将用户名赋值给成员变量
                    this.DialogResult=DialogResult.OK;  //将该窗体的返回值变为ok
                    this.Close();  //关闭该窗体

                }
                else { MessageBox.Show("用户名或密码错误!"); }
            }
            else { MessageBox.Show("用户名密码不能为空!"); }
        }
    }
}
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 登陆界面
{
    public partial class 主界面 : Form
    {
        private string uid;
        private DataOperation.LoginData lda;

        public 主界面()
        {
            InitializeComponent();
        }

        public 主界面(string username)
        {
            InitializeComponent();
            uid = username;
            lda = new DataOperation.LoginData();
        }

        private void 主界面_Load(object sender, EventArgs e)
        {
            label1.Text = uid;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace 登陆界面
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            //造一个登录窗体
            登陆界面.Login log = new Login();
            if (log.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new 主界面(log.username));                
            }
        }
    }
}

转载于:https://www.cnblogs.com/xiao55/p/5797734.html

相关文章:

  • Linux更换jdk版本,java -version还是原来的版本问题
  • zookeeper启动报错:-bash: zkServer.sh: command not found命令未找到
  • 算法笔记 --- 最短子数组
  • hadoop job 日志的查看
  • Jersey采用模板Freemarker输出
  • hadoop连接hdfs报错Call From master/172.27.0.5 to master:8020 failed on connection exception: 问题的解决
  • Linux命令手册
  • kernel:NMI watchdog: BUG: soft lockup - CPU#6 stuck for 28s! CentOS7linux中内核被锁死
  • reactjs服务器端渲染——node搭建简易服务器
  • java后台接收前端对象数组
  • MyBatis 中if 标签 判断字符串不生效
  • 开源大数据周刊-第20期
  • Linux新建Oracle用户和数据库并导入sql文件
  • layui 数据表格内嵌上传按钮,并在上传中增加所在行的id或其他属性
  • 重启oracle的方法
  • Android 架构优化~MVP 架构改造
  • angular组件开发
  • Facebook AccountKit 接入的坑点
  • Linux链接文件
  • Map集合、散列表、红黑树介绍
  • MySQL几个简单SQL的优化
  • npx命令介绍
  • oschina
  • Redux 中间件分析
  • SpingCloudBus整合RabbitMQ
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • Unix命令
  • web标准化(下)
  • 扑朔迷离的属性和特性【彻底弄清】
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 06-01 点餐小程序前台界面搭建
  • Java数据解析之JSON
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (1)(1.9) MSP (version 4.2)
  • (10)STL算法之搜索(二) 二分查找
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (C)一些题4
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (javascript)再说document.body.scrollTop的使用问题
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (未解决)macOS matplotlib 中文是方框
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (一)Linux+Windows下安装ffmpeg
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • (转)socket Aio demo
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • .net mvc部分视图
  • .NET 材料检测系统崩溃分析
  • .Net环境下的缓存技术介绍
  • .net经典笔试题