C# 作业,简单的学生管理系统(控制台)

需求描述

  • 构建一个Student类
    包括姓名、学号、分数,然后在控制台中输入N个学生的上述信息,并将信息保存到文件。
  • 创建一个文本文件
    将Student对象的信息写入到文本文件。如果该文件已存在则将该文件备份,再写入新的数据。
  • 对象序列化
    Student对象储存到ArrayList对象中,然后通过序列化,将ArrayList对象中的Student对象保存到文件中。
  • 读出文件中Student对象信息

思路分析

在这之前并不太了解C#的序列化,于是百度一下。
得一百度知道解答: c#中序列化是什么,怎么用,什么情况下用,不用有什么后果?

c#中序列化就是把一个对象保存到一个文件或数据库字段中去。
序列化用途:
1、在进程下次启动时读取上次保存的对象的信息
2、在不同的AppDomain或进程之间传递数据
3、在分布式应用系统中传递数据
常见的序列化的方法:
1、BinaryFormatter
2、SoapFormatter
3、XML序列化
用法:
  BinaryFormatter的用法大致如下: 

//BinaryFormatter将对象序列化到文件中
 List<string> inputList = new List<string>() { "str1","str2","str3"};
 using (FileStream fsWriter = new FileStream(@"tmp.dat",FileMode.Create,FileAccess.Write))
 {
       BinaryFormatter bf = new BinaryFormatter();
       //序列化
       bf.Serialize(fsWriter, inputList);
 }

 //BinaryFormatter将文件中的数据反序列化出来
 List<string> outputList = new List<string>();
 using (FileStream fsReader = new FileStream(@"tmp.dat",FileMode.Open,FileAccess.Read))
 {
       BinaryFormatter bf = new BinaryFormatter();
       //反序列化
       outputList = (List<string>)bf.Deserialize(fsReader);
 }
XML序列化的用法大致如下:
//xml序列化到tmp.xml文件中
List<string> inputList = new List<string>() { "str1","str2"};
using (FileStream fsWriter = new FileStream(@"tmp.xml",FileMode.Create,FileAccess.Write))
{
      XmlSerializer xs = new XmlSerializer(typeof(List<string>));
      xs.Serialize(fsWriter, inputList);
}

//从tmp.xml文件中反序列化出来
List<string> outputList = new List<string>();
using (FileStream fsReader = new FileStream(@"tmp.xml",FileMode.Open,FileAccess.Read))
{
     XmlSerializer xs = new XmlSerializer(typeof(List<string>));
     outputList = xs.Deserialize(fsReader) as List<string>;
}

总结:

两个的用法大致如下:

序列化:
  1.得到一个存储对象的类型
  2.创建一个写入文件流
  3.定义要序列化的类型
  4.调用序列化方法

反序列化:
  1.定义一个装载对象的类型
  2.创建一个读出文件流
  3.定义要反序列化的类型
  4.调用反序列化方法

BinaryFormatter类进行序列化和反序列化,以缩略型二进制格式写到一个文件中去,速度比较快,而且写入后的文件已二进制保存有一定的保密效果。标记为NonSerialized的其他所有成员都能序列化。

采用xml序列化的方式只能保存public的字段和可读写的属性,对于private等类型的字段不能进行序列化。

二进制序列化的优点:
  1. 所有的类成员(包括只读的)都可以被序列化;
  2. 性能非常好。

XML序列化的优点:
  1. 互操作性好;
  2. 不需要严格的二进制依赖;
  3. 可读性强

另外还受助于这片博文C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子

参考代码

Student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManager
{
    [Serializable]
    class Student
    {
        private string name;
        private int num;
        private int score;

        public Student()
        {
            name = "Anonymous";
            num = 0;
            score = 0;
        }

        public Student(string name, int num, int score)
        {
            this.name = name;
            this.num = num;
            this.score = score;
        }

        #region 访问器

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Num
        {
            get { return num; }
            set { num = value; }
        }

        public int Score
        {
            get { return score; }
            set { score = value; }
        }
        #endregion
    }
}

Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Configuration;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace StudentManager
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            string tempStr;
            Console.WriteLine("Input student's sum:");
            tempStr = Console.ReadLine();
            sum = Convert.ToInt32(tempStr);

            Console.WriteLine("sum = {0}",sum);
            ArrayList studentList = new ArrayList(sum);

            // input students' data
            for (int i = 1; i <= sum; i++)
            {
                string name;
                int num, score;
                Console.WriteLine("Input the data of student #{0}", i);
                Console.Write("Name: ");
                name = Console.ReadLine();
                Console.Write("Number: ");
                tempStr = Console.ReadLine();
                num = Convert.ToInt32(tempStr);
                Console.Write("Score: ");
                tempStr = Console.ReadLine();
                score = Convert.ToInt32(tempStr);

                var tempStudent = new Student(name, num, score);
                studentList.Add(tempStudent);
            }

            #region Serialize

            try
            {
                if (File.Exists(@"StudentData.txt"))
                {
                    File.Copy("StudentData.txt", "StudentData_tb.txt");
                    Console.WriteLine("StudentData.txt already exists, copied it to StudentData_tb.txt");
                }

                FileStream fs = new FileStream("StudentData.txt", FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, studentList);
                fs.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            Console.WriteLine("Serialize successful!");
            #endregion

            #region  Deserialize

            try
            {
                FileStream fs = new FileStream("StudentData.txt", FileMode.Open, FileAccess.Read);
                BinaryFormatter bf = new BinaryFormatter();
                ArrayList desList = (ArrayList) bf.Deserialize(fs);
                foreach (Student s in desList)
                {
                    Console.WriteLine(s.Name + " " + s.Num + " " + s.Score);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            #endregion
            Console.ReadKey();
        }
    }
}
  • 9
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vs2015+数据库,需要建的数据表如下: 1. “考试成绩管理系统用户登录”功能 具体要求: (1) 按照图示排列相应的控件,控件名称自定义,其中,界面中的图片可以不加; (2) 当输入正确的用户名和密码时,登录到主系统,如图所示,并且用户名或密码输入不正确时系统有所提示;当单击【取消】按钮时,用户名和密码被清空; (3) 程序中用到的数据库名为SCOREINFO,数据表名为userinfo,数据表结构如下图所示: (4) 数据表中的用户名和密码如下图。 2. 点击主窗体的“密码修改”菜单,完成“密码修改”功能,程序运行如下图所示: 具体要求: (1)此题必须使用数据库连接完成,原始密码必须为数据表里原有的数据,不使用数据库完成的为0分。 (2)需要建立数据库SCOREINFO及数据表userinfo,表的结构及数据第一部分的内容: (3)要有“原始密码输入错误”、“原始密码不能为空”及“两次输入密码不一致”的错误提示; (4)当单击【保存】按钮,新密码被更新到数据表中,不能更新的为0分; (5)单击【关闭】按钮,窗口关闭。 (6)3个Label;3个TextBox;2个Button 3. 完成“成绩查询”功能,程序运行如下图所示: 具体要求: (1) 按照图示排列相应的控件,界面下方是DataGridView控件; (2)程序用到的数据库名为SCOREINFO,数据表名为score,表结构如下: (3)完成的MainForm_Load事件处理程序:当加载窗体时,直接在窗体的dataGridView1控件中显示数据表的所有记录; (4)可以设查询条件:首先在组合框comboBox1中选择查询条件,并在textBox1中输入条件值(可以模糊查询,如按照姓名查询时,输入“王”,可以查所有姓王的同学的成绩),单击查询将结果显示在dataGridView1控件中。 (5)所需控件及属性:1个GroupBox,1个Label,Text为选择查询条件;1个ComboBox(Items:学号、姓名);1个TextBox;1个Button,Text为查询;1个DataGridView 4. 完成“课程信息修改”功能,程序运行如下图所示: 具体要求: (1)按照图示排列相应的控件,控件名称自定义,其中,程序刚开始运行时,“学分”和“课程编码”的文本框是只读的; (2)在数据库名为SCOREINFO中,创建数据表名为course,表结构如下: (3)当单击【查询】时,直接在窗体的dataGridView2控件中显示数据表的所有记录; (4)当选中DataGridView控件中的某一行记录时(DataGridView控件的Mouse_Click事件),“课程名字”、“学分”、“课程代码”文本框中分别显示该项对应的课程信息; (5)当选中某一行记录并单击【编辑】按钮时,【编辑】按钮变为【保存修改】,同时“学分”和“课程编码”的文本框恢复正常(ReadOnly属性为false);在文本框中修改相应的信息后单击【保存修改】,将修改后的数据更新到数据表中。 (6)所需控件及属性:1个GroupBox,3个Label;3个TextBox(textBox2属性ReadOnly为True,textBox3属性ReadOnly为True);2个Button;1个DataGridView 5. 完成“课程信息删除”功能,程序运行如下图所示: 具体要求: (1)按照图示排列相应的控件,控件名称自定义,其中,程序刚开始运行时,“学分”和“课程编码”的文本框是只读的; (2)数据表名为course,表结构同第4部分: (3)当单击【查询】时,直接在窗体的dataGridView控件中显示数据表的所有记录; (4)当选中DataGridView控件中的某一行记录时,“课程名字”、“学分”、“课程代码”文本框中分别显示该项对应的课程信息; (5)当选中某一行记录并单击【删除】按钮时,则该行从数据表中删除。 (6)所需控件:3个Label;3个TextBox(textBox2属性ReadOnly为True,textBox3属性ReadOnly为True);2个Button;1个DataGridView 6. 完成“课程信息添加”功能,程序运行如下图所示: 具体要求: (1)按照图示排列相应的控件,控件名称自定义; (2)程序用到的数据库和数据表名为course,表结构如下同第四部分: (3)当单击【查询】时,直接在窗体的dataGridView1控件中显示数据表的所有记 (4)当选中DataGridView控件中的某一行记录时,“课程名字”、“学分”、“课程代码”文本框中分别显示该项对应的课程信息; (5)当单击【添加】按钮时,在文本框中添加新的内容并将新内容添加到数据表中,并且在DataGridView控件中显示出新的课程信息 (6)所需控件:3个Label;3个TextBox;2个Button;1个DataGridView
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值