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 System.Data.OleDb;
namespace WindowsFormsApplication58
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int limit=0;//记录下用户的权限,以此来区别什么用户能打开什么菜单
private void button1_Click(object sender, EventArgs e)
{
//检索数据库里是否有该数据
string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb;";
OleDbConnection mycon=new OleDbConnection(str);
string sql="select * from 权限 where 账号= '"+ textBox1.Text +"' " + "and 密码= '"+ textBox2.Text+"' ";
OleDbCommand mycom = new OleDbCommand(sql, mycon);
mycon.Open();
OleDbDataReader reader = mycom.ExecuteReader();
if (reader.Read())
{
limit = Convert.ToInt32( textBox1.Text);
switch(limit)
{
case 1:
menu1.Enabled = true;break;
case 2:
menu2.Enabled = true;break;
case 3:
menu3.Enabled = true;break;
default:
MessageBox.Show("您没有使用本软件的权限!", "警告");break;
}
MessageBox.Show("登陆成功", "消息");
}
else
{
MessageBox.Show("登陆失败,请确保账号和密码无误", "消息");
}
}
private void Form1_Load(object sender, EventArgs e)
{
menu1.Enabled = false;
menu2.Enabled = false;
menu3.Enabled = false;
}
}
}
前提是要建立好密码账号的数据库。
效果图如下: