namespace StudentManagementSystem
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
public string code;
public static string EncryptWithMD5(string source)
{
byte[] sor = Encoding.UTF8.GetBytes(source);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
}
return strbul.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
string username = UserNametext.Text.Trim(); //取出账号
string password = EncryptWithMD5(Passwordtext.Text.Trim()); //取出密码并加密
//if (username == "admin")
//password = "123";//测试用例,便于初始化时候的 admin 密码 123可以顺利登陆。程序完成后可注释掉这行代码。
//string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //读取连接字符串
string myConnString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=";
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open();
string sql = "select UserID,UserPassword from SysUser where UserID = '" + username + "' and UserPassword = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows && textBox3.Text == code&&comboBox1.SelectedIndex==0)
{
common.id = UserNametext.Text.Trim();
MessageBox.Show("欢迎使用!"); //登录成功
StudentForm form2 = new StudentForm();
form2.Show();
this.Hide();
}
else if (sqlDataReader.HasRows && textBox3.Text == code && comboBox1.SelectedIndex == 1)
{
common.id = UserNametext.Text.Trim();
MessageBox.Show("欢迎使用!"); //登录成功
ManagerForm form2 = new ManagerForm();
form2.Show();
this.Hide();
}
else
{
MessageBox.Show("登录失败!");
code = null;
Random ran = new Random();
int number;
char code1;
//取五个数
for (int i = 0; i < 5; i++)
{
number = ran.Next();
if (number % 2 == 0)
code1 = (char)('0' + (char)(number % 10));
else
code1 = (char)('A' + (char)(number % 26)); //转化为字符
this.code += code1.ToString();
}
linkLabel1.Text = code;
return;
}
sqlDataReader.Close();
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void LoginForm_Load(object sender, EventArgs e)
{
Random ran = new Random();
int number;
char code1;
//取五个数
for (int i = 0; i < 5; i++)
{
number = ran.Next();
if (number % 2 == 0)
code1 = (char)('0' + (char)(number % 10));
else
code1 = (char)('A' + (char)(number % 26)); //转化为字符
this.code += code1.ToString();
}
linkLabel1.Text = code;
}
//点击验证码可以刷新
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
code = null;
Random ran = new Random();
int number;
char code1;
//取五个数
for (int i = 0; i < 5; i++)
{
number = ran.Next();
if (number % 2 == 0)
code1 = (char)('0' + (char)(number % 10));
else
code1 = (char)('A' + (char)(number % 26)); //转化为字符
this.code += code1.ToString();
}
linkLabel1.Text = code;
}
//点击注册按钮转到注册界面
private void button3_Click(object sender, EventArgs e)
{
SignForm signform = new SignForm();
signform.Show();
}
}
}
public static class common // static 不是必须
{
public static string id;
}
在使用学生账号登录时,打算用一个全局变量来存储其学号信息,但是经过查询资料,c#语言不支持全局变量的定义,所以在本次综合实验中,我使用的时定义一个新的common类来起到全局变量的作用。
public static class common // static 不是必须
{
public static string id;
}
namespace StudentManagementSystem
{
public partial class SignForm : Form
{
public SignForm()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
public Byte[] mybyte = new byte[0];
private void button3_Click(object sender, EventArgs e)
{
//打开浏览图片对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string picturePath = openFileDialog.FileName;//获取图片路径
//文件的名称,每次必须更换图片的名称,这里很为不便
//创建FileStream对象
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte, 0, mybyte.Length);
pictureBox2.Image = Image.FromStream(fs);
fs.Close();
}
public static string EncryptWithMD5(string source)
{
byte[] sor = Encoding.UTF8.GetBytes(source);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
}
return strbul.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
//string sql = "insert into SysUser (UserID, UserPassWord , UserSchoolID, UserMobile, UserBirthday , UserIdentity , UserPhoto ) " +
//"values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday,@useridentity,@userphoto)";
//SqlCommand command = new SqlCommand(sql, connection);
try
{
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
string sql = "insert into SysUser (UserID, UserPassWord , UserSchoolID, UserMobile, UserBirthday , UserIdentity , UserPhoto ) " +
"values (@userid, @userpassword,@userschoolid,@usermobile,@userbirthday,@useridentity,@userphoto)";
SqlCommand command = new SqlCommand(sql, connection);
SqlParameter sqlParameter = new SqlParameter("@userid", textBox1.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(textBox2.Text));
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userschoolid", textBox3.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@usermobile", textBox4.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userbirthday", dateTimePicker1.Value);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@useridentity", comboBox1.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userphoto", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, mybyte);
command.Parameters.Add(sqlParameter);
//打开数据库连接
connection.Open();
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("注册成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Close();
}
}
}
namespace StudentManagementSystem
{
public partial class StudentForm : Form
{
public StudentForm()
{
InitializeComponent();
}
private void ShowForm_Load(object sender, EventArgs e)
{
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string sql = "select UserPhoto from SysUser where UserID = '" + common.id + "'";
SqlCommand command = new SqlCommand(sql, con);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "SysUser");
int c = dataSet.Tables["SysUser"].Rows.Count;
if (c > 0)
{
Byte[] mybyte = new byte[0];
mybyte = (Byte[])(dataSet.Tables["SysUser"].Rows[c - 1]["UserPhoto"]);
MemoryStream ms = new MemoryStream(mybyte);
pictureBox1.Image = Image.FromStream(ms);
}
else
pictureBox1.Image = null;
con.Close();
}
private void button1_Click(object sender, EventArgs e)
{
LoginForm Form = new LoginForm();
Form.Show();
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
CourseForStudent cfs = new CourseForStudent();
cfs.Show();
}
}
}
namespace StudentManagementSystem
{
public partial class CourseForStudent : Form
{
public CourseForStudent()
{
InitializeComponent();
}
private void CourseForStudent_Load(object sender, EventArgs e)
{
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password= ";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
String select_by_id = "select * from SC where Sno=" + common.id;
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.SC”中。您可以根据需要移动或删除它。
//this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
namespace StudentManagementSystem
{
public partial class ManagerForm : Form
{
public ManagerForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LoginForm Form = new LoginForm();
Form.Show();
this.Close();
}
private void Manager_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
SysLog sys = new SysLog();
sys.Show();
}
private void button3_Click(object sender, EventArgs e)
{
SC sc = new SC();
sc.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Course course = new Course();
course.Show();
}
private void button5_Click(object sender, EventArgs e)
{
Student student = new Student();
student.Show();
}
}
}
namespace StudentManagementSystem
{
public partial class SysLog : Form
{
public SysLog()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void SysLog_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.SysLog”中。您可以根据需要移动或删除它。
this.sysLogTableAdapter.Fill(this.curricula_variable_systemDataSet.SysLog);
}
}
}
namespace StudentManagementSystem
{
public partial class Course : Form
{
public Course()
{
InitializeComponent();
}
private void Course_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.Course”中。您可以根据需要移动或删除它。
this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
String cno = Cno.Text;
String cname = Cname.Text;
String cpno = Cpno.Text;
String credit = Credit.Text;
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string insertStr = "INSERT INTO Course (Cno,Cname,Cpno,Ccredit) " +
"VALUES ('" + cno + "','" + cname + "','" + cpno + "','"+ credit + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求");
}
finally
{
con.Dispose();
}
this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);
}
private void button2_Click(object sender, EventArgs e)
{
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string delete_by_id = "delete from Course where Cno=" + cno;//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose();
}
this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);
}
private void button3_Click(object sender, EventArgs e)
{
String cno = Cno.Text.Trim();
String cname = Cname.Text.Trim();
String cpno = Cpno.Text.Trim();
String credit = Credit.Text.Trim();
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string insertStr = "UPDATE Course SET Cname = " + cname +",Cpno= "+cpno+",Ccredit="+credit +" WHERE Cno = '" + cno + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);
}
private void button4_Click(object sender, EventArgs e)
{
String cno = Cno.Text.Trim();
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
String select_by_id = "select * from Course where Cno='" + cno + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
}
}
}
namespace StudentManagementSystem
{
public partial class SC : Form
{
public SC()
{
InitializeComponent();
}
private void SC_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.SC”中。您可以根据需要移动或删除它。
this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
String Sno = SnoText.Text;
String Cno = CnoText.Text;
String Grade = GradeText.Text;
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string insertStr = "INSERT INTO SC (Sno,Cno,Grade) " +
"VALUES ('" + Sno + "','" + Cno + "','" + Grade + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求,该学生可能不在数据库中");
}
finally
{
con.Dispose();
}
this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);
}
private void button2_Click(object sender, EventArgs e)
{
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string cno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
string delete_by_id = "delete from SC where Sno=" + sno+"and Cno="+cno;//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose();
}
this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);
}
private void button3_Click(object sender, EventArgs e)
{
String sno = SnoText.Text.Trim();
String cno = CnoText.Text.Trim();
String grade = GradeText.Text.Trim();
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string insertStr = "UPDATE SC SET Grade = " + grade + " WHERE Sno = '" + sno + "'and Cno='"+cno+"'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);
}
private void button4_Click(object sender, EventArgs e)
{
String sno = SnoText.Text.Trim();
String cno = CnoText.Text.Trim();
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
String select_by_id = "select * from SC where Sno='" + sno + "'"+"and Cno='"+cno+"'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
}
}
}
namespace StudentManagementSystem
{
public partial class Student : Form
{
public Student()
{
InitializeComponent();
}
private void Student_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
String sno = Sno.Text;
String sname = Sname.Text;
String ssex = Ssex.Text;
String sage = Sage.Text;
String sdept = Sdept.Text;
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sage,Sdept) " +
"VALUES (" + sno + "," + sname + ",'" + ssex + "'," +sage+ ",'" + sdept + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);
}
private void button2_Click(object sender, EventArgs e)
{
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string sno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值,也就是ID
string delete_by_id = "delete from SC where Sno=" + sno+"delete from Student where Sno=" + sno ;//sql删除语句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);
}
private void button3_Click(object sender, EventArgs e)
{
String sno = Sno.Text.Trim();
String sname = Sname.Text.Trim();
String ssex = Ssex.Text.Trim();
String sage = Sage.Text.Trim();
String sdept = Sdept.Text.Trim();
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
string insertStr = "UPDATE Student SET Sname = " + sname + ",Ssex= '" + ssex + "',Sage=" + sage + ",Sdept='"+sdept+"' WHERE Sno = " + sno ;
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);
}
private void button4_Click(object sender, EventArgs e)
{
String sno = Sno.Text.Trim();
String connectionString = "Data Source=.;Initial Catalog=curricula_variable_system;User ID=sa;Password=";
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
String select_by_id = "select * from Student where Sno='" + sno + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, con);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询语句有误,请认真检查SQL语句!");
}
finally
{
con.Close();
}
}
}
}