正好我项目有这个,我随手粘贴过来吧,这个论坛真好,以后常来!
//确定 按钮事件处理
private void btok_Click(object sender, EventArgs e)
{
if (panduan())
//调用panduan方法
{
showUser();
}
}
//退出 按钮事件处理
private void btexit_Click(object sender, EventArgs e)
{
help.tuichu();
}
//登陆判断方法
private bool panduan()
{
if (txtLoginId.Text.Trim() == "")
{
MessageBox.Show("请输入用户名", "登陆提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtLoginId.Focus();
//光标定义位置
return false;
}
else if (txtLoginPwd.Text.Trim() == "")
{
MessageBox.Show("请输入密码", "登陆提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
txtLoginPwd.Focus();
return false;
}
else if (cboLoginType.Text.Trim() == "")
{
MessageBox.Show("请选择登陆类型", "登陆提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
else
{
return true;
}
}
//登陆类型方法
public void showUser()
{
switch (cboLoginType.Text)
{
case "管理员":
try
{
Admin();
//管理员权限
this.Visible = false;
//隐藏登陆窗体
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
DBHelper.connection.Close();
//关闭数据库
}
break;
case "学员":
try
{
xuesheng();
//学生权限
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
DBHelper.connection.Close();
//关闭数据库
}
break;
}
}
//管理员属性
private void Admin()
{
//数据库连接
DBHelper.connection.Open();
//创建sqlcommand对象去执行命令
string sql = string.Format("select count(*) from AdminLogin where AdminrName='{0}' and AdminPwd='{1}'",
this.txtLoginId.Text, this.txtLoginPwd.Text);
//执行命令
SqlCommand sqlcom = new SqlCommand(sql, DBHelper.connection);
int result = (int)sqlcom.ExecuteScalar();
if (result != 0)
{
help.user = this.txtLoginId.Text.Trim();
//把用户名字保存help类
help.Type = this.cboLoginType.Text.Trim();
//把登录类型保存help类
Form_Main Main = new Form_Main();
Main.Show();
this.Visible = false;
}
else
{
MessageBox.Show("用户名或密码错误!请重新输入");
}
}
//学员属性
private void xuesheng()
{
//查询语句
string sql = string.Format("select count(*) from StuInfo where StuNO='{0}' and StuPwd='{1}'", this.txtLoginId.Text.Trim(), this.txtLoginPwd.Text.Trim());
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
//创建 SqlCommand 对象
DBHelper.connection.Open();
//打开数据连接
int num = (int)command.ExecuteScalar();
//执行命令
if (num != 0)
{
help.Type = this.cboLoginType.Text.Trim();
help.user = this.txtLoginId.Text.Trim();
Form_Main Main = new Form_Main();
Main.Show();
}
else
{
MessageBox.Show("用户名或密码错误!请重新输入");
}
}