学生登陆页面
string username = textBox1.Text.Trim(); //取出账号
string password = EncryptWithMD5(textBox2.Text.Trim()); //取出密码并加密
string myConnString = "Data Source=.;Initial Catalog=Test;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)
{
GlobalData.passData = textBox1.Text;
MessageBox.Show("欢迎使用!"); //登录成功
Form15 form15 = new Form15(); //Form15为学生登陆后的页面
form15.Show();
this.Hide();
}
else
{
MessageBox.Show("登录失败!");
return;
}
sqlDataReader.Close();
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
MD5加密:
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();
}
验证码:
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();
}
label4.Text = code;
注册界面:
try
{
string connString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=";
SqlConnection connection = new SqlConnection(connString);
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", "Student");
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();
上传照片:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string picturePath = openFileDialog.FileName;
FileStream fs = new FileStream(picturePath, FileMode.Open, FileAccess.Read);
//声明Byte数组
mybyte = new byte[fs.Length];
//读取数据
fs.Read(mybyte, 0, mybyte.Length);
pictureBox1.Image = Image.FromStream(fs);
fs.Close();
学生页面(展示为登陆后的):
显示账号,学号:
textBox2.Text = GlobalData.passData; //有一个GlobalData类,用于登录页面和该页面传递数据
textBox2.ReadOnly = true;
String ID = textBox2.Text.Trim();
String conn = "Data Source=.;Initial Catalog=Test;User ID=sa;Password=";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
String select_by_id0 = "select UserSchoolID from SysUser where UserID='" + ID + "'";
SqlCommand sqlCommand0 = new SqlCommand(select_by_id0, sqlConnection);
textBox1.Text= sqlCommand0.ExecuteScalar().ToString();
textBox1.ReadOnly = true;
}
catch
{
}
finally
{
sqlConnection.Close();
}
显示照片:
try
{
string connString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=";
SqlConnection connection = new SqlConnection(connString);
connection.Open();
String sql = "select UserPhoto from SysUser where UserSchoolID = '" + textBox1.Text + "'";
SqlCommand command = new SqlCommand(sql, connection);
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;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
信息查询:
try
{
sqlConnection.Open();
String select_by_id = "select * from Student where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询错误");
}
finally
{
sqlConnection.Close();
}
成绩查询:
try
{
sqlConnection.Open();
String select_by_id = "select * from SC where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询错误");
}
finally
{
sqlConnection.Close();
}
管理员登录
string username = textBox1.Text.Trim();
string password = textBox2.Text.Trim();
string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=";
SqlConnection sqlConnection = new SqlConnection(myConnString);
sqlConnection.Open();
string sql = "select userid,password from usertable where userid = '" + username + "' and password = '" + password + "'";
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
MessageBox.Show("WELCOME!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Form4 form4 = new Form4();
form4.Show();
this.Hide();
}
else
{
MessageBox.Show("FAILED!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
sqlConnection.Close();
学生信息管理:
增加:
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuAge = textBox4.Text.Trim();
String StuSdept = textBox5.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=");
try
{
con.Open();
string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) " +
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuSdept + "'," + StuAge + ")";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.testDataSet2.Student);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
删除:
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string delete_by_id1 = "delete from SC where Sno=" + select_id;
SqlCommand cmd1 = new SqlCommand(delete_by_id1, con);
string delete_by_id = "delete from Student where Sno=" + select_id;
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd1.ExecuteNonQuery();
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.testDataSet3.Student);
修改:
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuAge = textBox4.Text.Trim();
String StuSdept = textBox5.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=");
try
{
con.Open();
string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";
string insertStr1 = "UPDATE Student SET Ssex = '" + StuSex + "' WHERE Sno = '" + StuID + "'";
string insertStr2 = "UPDATE Student SET Sage = '" + StuAge + "' WHERE Sno = '" + StuID + "'";
string insertStr3 = "UPDATE Student SET Sdept = '" + StuSdept + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
SqlCommand cmd1 = new SqlCommand(insertStr1, con);
SqlCommand cmd2 = new SqlCommand(insertStr2, con);
SqlCommand cmd3 = new SqlCommand(insertStr3, con);
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
查询:(按姓名查询和以下类似)
String StuID = textBox1.Text.Trim();
String conn = "Data Source=.;Initial Catalog=Test;User ID=sa;Password=";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
String select_by_id = "select * from Student where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询错误");
}
finally
{
sqlConnection.Close();
}
成绩管理
增:
String StuSno = textBox1.Text.Trim();
String StuCno = textBox2.Text.Trim();
String StuGrade = textBox3.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=");
try
{
con.Open();
string insertStr = "INSERT INTO SC (Sno,Cno,Grade) " +
"VALUES ('" + StuSno + "','" + StuCno + "'," + StuGrade + ")";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.sCTableAdapter.Fill(this.testDataSet8.SC);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
删:
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string select_id1 = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
string delete_by_id = "delete from SC where Sno=" + select_id+"AND Cno=" + select_id1;
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("请正确选择行!");
}
finally
{
con.Dispose();
}
改:
String StuID = textBox1.Text.Trim();
String SCno = textBox2.Text.Trim();
String SGrade = textBox3.Text.Trim();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=");
try
{
con.Open();
string insertStr = "UPDATE SC SET Grade = " + SGrade + "WHERE Sno = " + StuID + "AND Cno="+SCno;
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("输入数据违反要求!");
}
finally
{
con.Dispose();
}
this.sCTableAdapter.Fill(this.testDataSet11.SC);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
查:
String StuID = textBox1.Text.Trim();
String conn = "Data Source=.;Initial Catalog=Test;User ID=sa;Password=";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
String select_by_id = "select * from SC where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查询错误");
}
finally
{
sqlConnection.Close();
}