登陆,激活,权限

连接数据库

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

namespace 登陆_激活_权限.Model
{
    public class Login
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public bool State { get; set; }
        public string Permissions { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 登陆_激活_权限.Model;
using System.Data.SqlClient;

namespace 登陆_激活_权限.DataOperation
{
    public class LoginData
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        public LoginData()
        {
            conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;");
            cmd = conn.CreateCommand();
        }

        public Login SelectUser(string Uname, string Pwd)
        {
            Login log = null;
            cmd.CommandText = "select *from [Login] where Username=@u and Password = @p";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@u", Uname);
            cmd.Parameters.Add("@p", Pwd);

            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                log = new Login();
                dr.Read();
                log.Username = dr["Username"].ToString();
                log.Password = dr["Password"].ToString();
                log.State = Convert.ToBoolean(dr["State"]);
                log.Permissions = dr["Permissions"].ToString();
            }

            conn.Close();
            return log;
        }


    }
}

表单设置

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 登陆_激活_权限.DataOperation;
using 登陆_激活_权限.Model;

namespace 登陆_激活_权限
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //1、验证用户名
            string name = textBox1.Text;
            string pwd = textBox2.Text;

            Login loo = new LoginData().SelectUser(name, pwd);
            if (loo == null)
            {
                MessageBox.Show("账户不存在!");
            }
            else
            { 
                //1、判断账户是否激活
                if (loo.State)
                {
                    Form2 f2 = new Form2(this,loo);
                    f2.Show();
                    this.Hide();
                }
                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;
using 登陆_激活_权限.Model;

namespace 登陆_激活_权限
{
    public partial class Form2 : Form
    {
        Form1 F1 = null;
        public Form2(Form1 f1,Login looo)
        {
            InitializeComponent();
            F1 = f1;
            string[] aaa = looo.Permissions.Split(',');

            if (aaa.Contains("101"))
            {
                button1.Visible = true;
            }
            if (aaa.Contains("102"))
            {
                button2.Visible = true;
            }
            if (aaa.Contains("103"))
            {
                button3.Visible = true;
            }
            
            

        }
    }
}
原文地址:https://www.cnblogs.com/zhangdemin/p/5681618.html