C#学生信息管理系统-代码展示

编程环境:SQL Server 2019
Visual Studio 2019

(仅功能代码)

  • 登陆
    在这里插入图片描述
private void button2_Click(object sender, EventArgs e)  //登陆
        {
   
            
                string username = textBoxUserName.Text.Trim();  //取出账号
                string password = EncryptWithMD5(textBoxPassWord.Text.Trim());  //取出密码
                name  =  username;
            
                //string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //读取连接字符串
                string myConnString = "Data Source=.;Initial Catalog=Text;Persist Security Info=True;User ID=sa;Password=110023";  //连接字符串 Data source为服务器的名字  Text为连接的库

                SqlConnection sqlConnection = new SqlConnection(myConnString);  //实例化连接对象
                sqlConnection.Open();

                string sql = "select UserID,UserPassword from UserMessage where UserID = '" + username + "' and UserPassword = '" + password + "'";                                            //编写SQL命令
               
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            
             string select_by_id="select * from UserMessage where UserID='" + username+"'";
            
            if (sqlDataReader.HasRows && radioButton1.Checked && txtVerifyCode.Text.Equals(VerifyCode, StringComparison.OrdinalIgnoreCase))        //学生成功登录
            {
   
                MessageBox.Show("欢迎使用!");             //登录成功
                FormStudent formstudent = new FormStudent();
                formstudent.Show();
                //this.Hide();
            }
            if (sqlDataReader.HasRows && radioButton2.Checked && txtVerifyCode.Text.Equals(VerifyCode, StringComparison.OrdinalIgnoreCase))      //管理员成功登录
            {
   
                MessageBox.Show("欢迎使用!");             //登录成功
                FormAdmini formAdmini = new FormAdmini();
                formAdmini.Show();
                // this.Hide();
            }

            if (textBoxUserName.Text.Trim() == "")
            {
   
                MessageBox.Show("请输入学号/工号!");
            }
            if (textBoxPassWord.Text.Trim() == "")
            {
   
                MessageBox.Show("请输入密码!");
            }
            //验证码
            if (string.IsNullOrEmpty(txtVerifyCode.Text))
            {
   
                MessageBox.Show("请输入验证码!");
                return;
            }
            if (sqlDataReader.HasRows )
                {
   
                //label1.Text = "Log in :" + username;
                if (txtVerifyCode.Text.Equals(VerifyCode, StringComparison.OrdinalIgnoreCase))
                {
   
                    label1.Text = "Log in :" + username;

                }
                else
                {
   
                    MessageBox.Show("验证码错误!请重新输入!");
                }
            }
                else if(!sqlDataReader.Read() && textBoxPassWord.Text.Trim() !="" && textBoxUserName.Text.Trim() != "")
                {
   
                    MessageBox.Show("密码输入错误!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                sqlConnection.Close();
            
        }

验证码

private string MakeCode(int codeLen)
        {
   
            if (codeLen < 1)
            {
   
                return string.Empty;
            }

            int number;
            string checkCode = string.Empty;
            Random random = new Random();

            for (int index = 0; index < codeLen; index++)
            {
   

                number = random.Next();

                if (number % 2 == 0)
                {
   
                    checkCode += (char)('0' + (char)(number % 10));     //生成数字
                }
                else
                {
   
                    checkCode += (char)('A' + (char)(number % 26));     //生成字母
                }
            }

            return checkCode;
        }
        private Image CreateCodeImg(string checkCode)
        {
   
            if (string.IsNullOrEmpty(checkCode))
            {
   
                return null;
            }

            Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);

            Graphics graphic = Graphics.FromImage(image);

            try
            {
   
                Random random = new Random();

                graphic.Clear(Color.White);

                int x1 = 0, y1 = 0, x2 = 0, y2 = 0;

                for (int index = 0; index < 25; index++)
                {
   
                    x1 = random.Next(image.Width);
                    x2 = random.Next(image.Width);
                    y1 = random.Next(image.Height);
                    y2 = random.Next(image.Height);

                    graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
                graphic.DrawString(checkCode, font, brush, 2, 2);

                int x = 0;
                int y = 0;

                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
   
                    x = random.Next(image.Width);
                    y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }

                //画图片的边框线
                graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                return image;

            }
            finally
            {
   
                graphic.Dispose();
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
   
            VerifyCode = MakeCode(5);
            pictureBox1.Image = CreateCodeImg(VerifyCode);
        }

密码加密

public static string EncryptWithMD5(string source)      //MD5加密
        {
   
            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 buttonOK_Click(object sender, EventArgs e)//注册成功
        {
   
            try
            {
   
                string connString = "Data Source =.; Initial Catalog = Text;  Persist Security Info = True;User ID = sa; Password =110023";//数据库连接字符串
                SqlConnection connection = new SqlConnection(connString);//创建connection对象
                string sql = "insert into UserMessage (UserName, UserID ,   UserPassword, UserIdentity ,UserSex, UserNumber ,UserBirthday ,UserPhoto ) " +
                                                        "values (@username, @userid, @userpassword,@useridentity,@usersex,@usernumber,@userbirthday,@userphoto)";
                SqlCommand command = new SqlCommand(sql, connection);

                
                SqlParameter sqlParameter = new SqlParameter("@userid", textBoxNum.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@username", textBoxName.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(textBoxPassword.Text));
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@usersex", comboBoxsex.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@usernumber", textBoxNumber.Text);
                command.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@userbirthday"
  • 47
    点赞
  • 316
    收藏
    觉得还不错? 一键收藏
  • 41
    评论
以下是一个简单的学生信息管理系统C# 代码示例,包括基本的添加、删除、修改和查询功能。代码中使用了一个 Student 类来存储学生信息,使用 List<Student> 来存储所有学生信息。 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StudentManagementSystem { class Program { static List<Student> students = new List<Student>(); static void Main(string[] args) { while (true) { Console.WriteLine("请选择操作:"); Console.WriteLine("1. 添加学生信息"); Console.WriteLine("2. 修改学生信息"); Console.WriteLine("3. 删除学生信息"); Console.WriteLine("4. 查询学生信息"); Console.WriteLine("5. 退出"); Console.Write("请选择:"); string choice = Console.ReadLine(); switch (choice) { case "1": AddStudent(); break; case "2": ModifyStudent(); break; case "3": DeleteStudent(); break; case "4": QueryStudent(); break; case "5": return; default: Console.WriteLine("无效的选择,请重新输入。"); break; } } } static void AddStudent() { Console.WriteLine("请输入学生信息:"); Console.Write("学号:"); string id = Console.ReadLine(); Console.Write("姓名:"); string name = Console.ReadLine(); Console.Write("性别:"); string gender = Console.ReadLine(); Console.Write("年龄:"); int age = int.Parse(Console.ReadLine()); Console.Write("籍贯:"); string hometown = Console.ReadLine(); Student student = new Student(id, name, gender, age, hometown); students.Add(student); Console.WriteLine("学生信息添加成功。"); } static void ModifyStudent() { Console.WriteLine("请输入要修改的学生信息:"); Console.Write("学号:"); string id = Console.ReadLine(); Student student = students.Find(s => s.ID == id); if (student == null) { Console.WriteLine("未找到该学生信息。"); return; } Console.WriteLine("请输入要修改的信息(不修改的信息直接回车):"); Console.Write("姓名:"); string name = Console.ReadLine(); if (!string.IsNullOrEmpty(name)) { student.Name = name; } Console.Write("性别:"); string gender = Console.ReadLine(); if (!string.IsNullOrEmpty(gender)) { student.Gender = gender; } Console.Write("年龄:"); string ageStr = Console.ReadLine(); if (!string.IsNullOrEmpty(ageStr)) { int age = int.Parse(ageStr); student.Age = age; } Console.Write("籍贯:"); string hometown = Console.ReadLine(); if (!string.IsNullOrEmpty(hometown)) { student.Hometown = hometown; } Console.WriteLine("学生信息修改成功。"); } static void DeleteStudent() { Console.WriteLine("请输入要删除的学生信息:"); Console.Write("学号:"); string id = Console.ReadLine(); Student student = students.Find(s => s.ID == id); if (student == null) { Console.WriteLine("未找到该学生信息。"); return; } students.Remove(student); Console.WriteLine("学生信息删除成功。"); } static void QueryStudent() { Console.WriteLine("请输入要查询的学生信息:"); Console.Write("学号:"); string id = Console.ReadLine(); Student student = students.Find(s => s.ID == id); if (student == null) { Console.WriteLine("未找到该学生信息。"); return; } Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},籍贯:{4}", student.ID, student.Name, student.Gender, student.Age, student.Hometown); } } class Student { public string ID { get; set; } public string Name { get; set; } public string Gender { get; set; } public int Age { get; set; } public string Hometown { get; set; } public Student(string id, string name, string gender, int age, string hometown) { ID = id; Name = name; Gender = gender; Age = age; Hometown = hometown; } } } ```
评论 41
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值