这是一段登录的代码
private void button1_Click(object sender,EventArgs e)
{
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
{
MessageBox.Show("请输入用户名和密码","错误提示");
}
else //有帐号密码的情况下,去数据库验证
{
DB.cn.Open(); //打开数据库
OleDbCommand cmd = new OleDbCommand("",DB.cn);
string sql = "select RoleName from UserInfo where user='" + textBox1.Text.Trim() + "' and Password='" + textBox2.Text.Trim() + "'";//查询用户帐号的sql
cmd.CommandText = sql;
string rolename;
if (null != cmd.ExecuteScalar())//查询结果不为空,说明帐号登录成功
{
rolename = cmd.ExecuteScalar().ToString();//查询SQL返回了角色名
this.Visible = false;//隐藏登录窗
Main main = new Main();//初始化main窗体
main.Tag = this.FindForm();
sql = "select * from Roles where RoleName='" + rolename + "'";//拿当前角色的所有信息的sql,包括权限,下面用到
cmd.CommandText = sql;
OleDbDataReader xq = cmd.ExecuteReader();//执行sql
xq.Read();
main.XTMenuItem.Visible = (bool)(xq.GetValue(1));//用角色的配置权限,初始化菜单
main.CJMenuItem.Visible = (bool)(xq.GetValue(2));
main.BJMenuItem.Visible = (bool)(xq.GetValue(4));
main.KCMenuItem.Visible = (bool)(xq.GetValue(5));
main.DAMenuItem.Visible = (bool)(xq.GetValue(6));
main.StatusLabel2.Text = textBox1.Text.Trim();//当前用户的角色名放进label显示.
main.ShowDialog();//显示主窗口
DB.cn.Close();//关闭数据库连接
}
else//查询角色帐号密码的sql没有返回数据
{
MessageBox.Show("用户名或密码错误","登录失败");
DB.cn.Close();
}
}