首先附加数据库,管理员账号 admin 密码 00000教师账号 t001 密码 12345学生账号 SunPeng 密码 12345
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;//下载自:找例子网 https://www.5t6t.comnamespace LoginFrame{ public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); } private void pictureBox2_Click(object sender, EventArgs e) { Application.Exit(); } private void pictureBox1_Click(object sender, EventArgs e) { if (ValidateInput()) { string userName = txtUserName.Text; string pwd = txtPwd.Text; try { string sql = ""; if (cbxUserStyle.Text == "教员") { sql =string.Format( "select count(*) from Teacher where LoginId='{0}' and LoginPwd='{1}'",userName,pwd); } else if (cbxUserStyle.Text == "管理员") { sql = string.Format("select count(*) from Admin where LoginId='{0}' and LoginPwd='{1}'",userName,pwd); } else {//学生 sql = string.Format("select count(*) from Student where LoginId='{0}' and LoginPwd='{1}'", userName, pwd); } SqlCommand command = new SqlCommand(sql,DbHelper.connection); DbHelper.connection.Open(); int i = (int)command.ExecuteScalar(); if (i > 0) { if (cbxUserStyle.Text == "教员") { SirForm sf = new SirForm(); sf.FormClosed = Sf_FormClosed; sf.Show(); this.Hide(); } else if (cbxUserStyle.Text == "管理员") { AdminForm af = new AdminForm(); af.FormClosed = Sf_FormClosed; af.Show(); this.Hide(); } else { StuForm sf = new StuForm(); sf.FormClosed = Sf_FormClosed; sf.Show(); this.Hide(); } MessageBox.Show("登陆成功!"); } else { MessageBox.Show("用户名或密码不正确!"); txtPwd.Text = ""; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DbHelper.connection.Close(); } } } private void Sf_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit(); } private bool ValidateInput() { if (txtUserName.Text.Trim() == "") { MessageBox.Show("请输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } else if (txtPwd.Text.Trim() == "") { MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } else if (cbxUserStyle.Text.Trim() == "") { MessageBox.Show("请选择用户类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } return true; } private void LoginForm_Load(object sender, EventArgs e) { //string sql = "select * from UserType"; //DataSet dataSet = new DataSet(); //SqlDataAdapter dataAdapter = new SqlDataAdapter(sql,DbHelper.connection); //try //{ // dataAdapter.Fill(dataSet, "UserType"); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); // return; //} //cbxUserStyle.DataSource = dataSet.Tables[0]; cbxUserStyle.SelectedIndex = 0;//默认选择第一项 } }}
评论