做完啦~实验报告的截图也整好啦!就剩程序演示还没整了,耶咦!
为了方便观看,每个窗体的代码头部这部分(类似下边这个)我都没有粘贴,只贴了点击事件和重要操作的代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void Form16_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“sTUDENTDataSet11.SC”中。您可以根据需要移动或删除它。
this.sCTableAdapter.Fill(this.sTUDENTDataSet11.SC);
}
FORM4:选择登录&注册界面
private void button1_Click(object sender, EventArgs e)//管理员登录
{
Form1 form1 = new Form1();
form1.Show();//显示form1
}
private void button2_Click(object sender, EventArgs e)//普通用户登录
{
Form5 form5 = new Form5();
form5.Show();//显示form5
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)//用户注册
{
Form12 form12 = new Form12();
form12.ShowDialog();//显示form12
}
FORM12:注册
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);
pictureBox1.Image = Image.FromStream(fs);
fs.Close();
}
private void button2_Click(object sender, EventArgs e)//取消
{
this.Close();//关闭
}
private void button1_Click(object sender, EventArgs e)//确定
{
Form8 form8 = new Form8();
if (comboBox1.Text.Trim() == "学生")//如果是学生注册,添加到SysUsers
{
try{
string connString = "Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou";//数据库连接字符串
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);
}
}
if(comboBox1.Text.Trim() == "管理员")//如果是管理员注册,添加到SyUsers
{
try{
string connString = "Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
string sql = "insert into SyUser (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();//界面关闭
}
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 TextBox1_Leave(object sender, EventArgs e)//用户名格式
{
if (textBox1.Text.Trim() != "")
{
//使用regex进行格式设置 至少含有一个大写字母。最少3个字符、最长20个字符。
Regex regex = new Regex(@"(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox1.Text))//判断格式是否符合要求
{
}
else
{
MessageBox.Show("至少含有一个大写字母。最少3个字符、最长20个字符!");
textBox1.Focus();
}
}
else
{
MessageBox.Show("Please fill in the full information!");
}
}
private void TextBox2_Leave(object sender, EventArgs e)//密码格式
{
if (textBox2.Text.Trim() != "")
{
//使用regex进行格式设置 至少含有一个数字。最少3个字符、最长20个字符。
Regex regex = new Regex(@"(?=.*[0-9]).{3,20}");
if (regex.IsMatch(textBox2.Text))//判断格式是否符合要求
{
}
else
{
MessageBox.Show("最少3个字符、最长20个字符!");
textBox2.Focus();
}
}
else
{
MessageBox.Show("Please fill in the full information!");
}
}
FORM1:管理员登录
private void button1_Click_1(object sender, EventArgs e)//登录
{
string username = textBox1.Text.Trim(); //取出账号
J.j= textBox1.Text.Trim();//记录id
string password = EncryptWithMD5(textBox2.Text.Trim()); //取出密码并加密
string myConnString = "Data Source=.;Initial Catalog=STUDENT;Persist Security Info=True;User ID=sa;Password=tangdou"; //数据库连接
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open();
string sql = "select UserID,UserPassword from SyUser where UserID = '" + username + "' and UserPassword = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection); //在数据库上执行SELECT操作
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); //读取
Form3 form3 = new Form3(); //实例化
Form13 form13 = new Form13();
if (sqlDataReader.HasRows && textBox3.Text == code) //验证码,用户名,密码都正确 登录成功
{
label1.Text = "登录成功:" + username;
form13.Show(); //显示13
}
else //登录失败
{
form3.Show();//显示登录失败窗口
}
sqlDataReader.Close();
sql = "insert into SysLog values ( '" + username + "' , '" + DateTime.Now + "' , '" + "Login" + "')"; //编写SQL命令,添加登录记录
sqlCommand = new SqlCommand(sql, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
private 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"结