练习: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