Test
验证码:
public string code;
private void Form1_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();
}
label5.Text = code;
}
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();
}
Register:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Register register = new Register();
register.ShowDialog();
}
OK:
private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text.Trim(); //取出账号
string password = EncryptWithMD5(textBox2.Text.Trim()); //取出密码并加密
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)
{
MessageBox.Show("欢迎使用!"); //登录成功
Main form2 = new Main();
form2.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();
}
User ID:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Trim() != "")
{
//使用regex(正则表达式)进行格式设置 至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符。
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(textBox1.Text))//判断格式是否符合要求
{
//MessageBox.Show("输入密码格式正确!");
}
else
{
MessageBox.Show("至少有数字、大写字母、小写字母各一个。最少3个字符、最长20个字符!");
textBox1.Focus();
}
}
else
{
MessageBox.Show("Please fill in the full information!");
}
}
Upload Image:
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();
}
OK:
private void button1_Click(object sender, EventArgs e)
{
try
{
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;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("register succeed");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Close();
}
private void Main_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.SC”中。您可以根据需要移动或删除它。
this.sCTableAdapter.Fill(this.curricula_variable_systemDataSet.SC);
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.Course”中。您可以根据需要移动或删除它。
this.courseTableAdapter.Fill(this.curricula_variable_systemDataSet.Course);
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.Student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.curricula_variable_systemDataSet.Student);
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.SysLog”中。您可以根据需要移动或删除它。
this.sysLogTableAdapter.Fill(this.curricula_variable_systemDataSet.SysLog);
// TODO: 这行代码将数据加载到表“curricula_variable_systemDataSet.SysUser”中。您可以根据需要移动或删除它。
this.sysUserTableAdapter.Fill(this.curricula_variable_systemDataSet.SysUser);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string connString = "Data Source=.;Initial Catalog=curricula_variable_system;Persist Security Info=True;User ID=sa;Password=***";//数据库连接字符串
SqlConnection connection = new SqlConnection(connString);//创建connection对象
//打开数据库连接
connection.Open();
//创建SQL语句
string sql = "select UserPhoto from SysUser where UserID = '" + textBox1.Text + "'";
//创建SqlCommand对象
SqlCommand command = new SqlCommand(sql, connection);
//创建DataAdapter对象
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
//创建DataSet对象
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);
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
this.sysLogTableAdapter.Fill(this.curricula_variable_systemDataSet.SysLog);
}
学生管理系统
登录SysLog
列名 | 说明 | 数据类型 | 约束 |
---|---|---|---|
UserID | 用户名 | nchar(20) | |
DateAndTime | 时间 | datetime | |
UserOperation | 操作 | nchar(200) |
管理员SysUser
列名 | 说明 | 数据类型 | 约束 |
---|---|---|---|
ID | 用户名 | char(20) | PRIMARY KEY |
UserPassWord | 密码 | char(32) | NOT NULL |
学生Student
列名 | 说明 | 数据类型 | 约束 |
---|---|---|---|
Sno | 学号 | char(20) | PRIMARY KEY |
Sname | 姓名 | char(20) | |
Ssex | 性别 | char(10) | |
Sbirthyear | 生日 | int | |
Sdept | 所在系 | char(20) | |
UserPassWord | 密码 | char(32) |
课程Course
列名 | 说明 | 数据类型 | 约束 |
---|---|---|---|
Cno | 课程号 | char(20) | PRIMARY KEY |
Cname | 课程名 | char(20) | NOT NULL |
选修SC
列名 | 说明 | 数据类型 | 约束 |
---|---|---|---|
Sno | 学号 | char(20) | PRIMARY KEY ,REFERENCES |
Cno | 课程号 | char(20) | PRIMARY KEY ,REFERENCES |
Grade | 成绩 | int |