一、步骤
1.创建数据库
use bosamvs go create table [Login] ( Username nvarchar(100), [Password] nvarchar(100), [State] bit, [Permissions] nvarchar(max) ) insert into [Login] values('zhangsan','1234',1,'101,102,103') insert into [Login] values('lisi','1234',1,'101') insert into [Login] values('wangwu','1234',0,'101,102,103') select*from [Login]
2.封装数据库
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=bosamvs;user=sa;pwd=123456;"); 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; } } }
3.登陆、激活、权限验证
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("账户未激活!无法登陆!"); } } } } }