2楼代码如下
class Program
{
static void Main(string[] args)
{
LoadInfo();
LoadDataSet();
Input();
}
private static void LoadInfo()
{
Console.WriteLine("**********通迅录**********");
Console.WriteLine("1 新增");
Console.WriteLine("2 修改");
Console.WriteLine("3 删除");
Console.WriteLine("4 显示");
Console.WriteLine("5 查找");
Console.WriteLine("6 保存");
Console.WriteLine("7 退出");
Console.WriteLine("**************************");
}
private static void LoadDataSet()
{
Oper.ds = new DataSet();
Oper.initDataSet();
ClsSerial serial = new ClsSerial();
Oper oper = (Oper)serial.DeSerializeNow(Application.StartupPath + "\\stu.dll");
if (oper != null && oper.list.Count > 0)
{
IEnumerator ie = oper.list.GetEnumerator();
while (ie.MoveNext())
{
Student stu = (Student)ie.Current;
DataTable dt = Oper.ds.Tables["student"];
dt.Rows.Add(new string[] { stu.StuNum, stu.StuName, stu.StuPhoneNum, stu.StrAddress });
}
}
}
private static void Input()
{
bool b = false;
while (true)
{
string str = Console.ReadLine();
if (str == "1")
Add();
else if (str == "2")
Edit();
else if (str == "3")
Del();
else if (str == "4")
Show();
else if (str == "5")
Find();
else if (str == "6")
Save();
else if (str == "7")
Exit(out b);
if (b)
{
return;
}
}
}
private static void Add()
{
Console.WriteLine("***********新增***********");
Student stu = new Student();
Console.Write("学号:");
stu.StuNum = Console.ReadLine();
Console.Write("姓名:");
stu.StuName = Console.ReadLine();
Console.Write("电话:");
stu.StuPhoneNum = Console.ReadLine();
Console.Write("地址:");
stu.StrAddress = Console.ReadLine();
DataTable dt = Oper.ds.Tables["student"];
try
{
dt.Rows.Add(new string[] { stu.StuNum, stu.StuName, stu.StuPhoneNum, stu.StrAddress });
}
catch (ConstraintException ex)
{
Console.WriteLine("学号重复!");
Add();
}
Console.WriteLine("**************************");
}
private static void Edit()
{
DataTable dt = Oper.ds.Tables["student"];
if (dt.Rows.Count > 0)
{
Console.WriteLine("***********修改***********");
Console.Write("需要修改的学生(学号):");
string Num = Console.ReadLine();
DataRow[] dr = dt.Select("StuNum = " + Num);
if (dr.Length > 0)
{
Console.Write("姓名:");
dr[0]["StuName"] = Console.ReadLine();
Console.Write("电话:");
dr[0]["StuPhoneNum"] = Console.ReadLine();
Console.Write("地址:");
dr[0]["StrAddress"] = Console.ReadLine();
}
else
{
Console.WriteLine("通迅录中未找到该学生!");
}
}
else
{
Console.WriteLine("通迅录中没有学生!");
}
Console.WriteLine("**************************");
}
private static void Del()
{
DataTable dt = Oper.ds.Tables["student"];
if (dt.Rows.Count > 0)
{
Console.WriteLine("***********修改***********");
Console.Write("需要删除的学生(学号):");
string Num = Console.ReadLine();
DataRow[] dr = dt.Select("StuNum = " + Num);
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
}
else
{
Console.WriteLine("通迅录中未找到该学生!");
}
}
else
{
Console.WriteLine("通迅录中没有学生!");
}
Console.WriteLine("**************************");
}
private static void Show()
{
Console.WriteLine("***********显示***********");
Console.WriteLine("学号\t\t姓名\t\t电话\t\t地址");
DataTable dt = Oper.ds.Tables["student"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Console.WriteLine(dt.Rows[i]["StuNum"] + "\t\t" + dt.Rows[i]["StuName"] + "\t\t" + dt.Rows[i]["StuPhoneNum"] + "\t\t" + dt.Rows[i]["StrAddress"]);
}
}
Console.WriteLine("**************************");
}
private static void Find()
{
DataTable dt = Oper.ds.Tables["student"];
if (dt.Rows.Count > 0)
{
Console.WriteLine("***********查找***********");
Console.Write("需要查找的学生(学号):");
string Num = Console.ReadLine();
DataRow[] dr = dt.Select("StuNum = " + Num);
if (dr.Length > 0)
{
Console.WriteLine("学号\t\t姓名\t\t电话\t\t地址");
Console.WriteLine(dr[0]["StuNum"] + "\t\t" + dr[0]["StuName"] + "\t\t" + dr[0]["StuPhoneNum"] + "\t\t" + dr[0]["StrAddress"]);
}
else
{
Console.WriteLine("通迅录中未找到该学生!");
}
}
else
{
Console.WriteLine("通迅录中没有学生!");
}
Console.WriteLine("**************************");
}
private static void Save()
{
string path = Application.StartupPath + "\\stu.dll";
Oper oper = new Oper();
oper.AddList();
ClsSerial serial = new ClsSerial(path, oper);
serial.SerializeNow();
Console.WriteLine("*********保存成功*********");
}
private static void Exit(out bool b)
{
Console.Write("所有未保存的数据将丢失,确认退出(Y/N)");
string str = Console.ReadLine();
b = false;
if (str == "Y" || str == "y")
{
b = true;
}
}
}
[Serializable]
public class Student
{
public string StuNum { get; set; }
public string StuName { get; set; }
public string StuPhoneNum { get; set; }
public string StrAddress { get; set; }
}
[Serializable]
public class Oper
{
public static DataSet ds = null;
public ArrayList list = null;
public static void initDataSet()
{
DataTable dt = new DataTable();
dt.TableName = "student";
dt.Columns.Add("StuNum");
dt.Columns.Add("StuName");
dt.Columns.Add("StuPhoneNum");
dt.Columns.Add("StrAddress");
dt.PrimaryKey = new DataColumn[] { dt.Columns["StuNum"] };
if (ds != null)
{
ds.Tables.Add(dt);
}
}
public void AddList()
{
if (ds != null)
{
DataTable dt = ds.Tables["student"];
list = new ArrayList();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Student stu = new Student();
stu.StuNum = dt.Rows[i]["StuNum"].ToString();
stu.StuName = dt.Rows[i]["StuName"].ToString();
stu.StuPhoneNum = dt.Rows[i]["StuPhoneNum"].ToString();
stu.StrAddress = dt.Rows[i]["StrAddress"].ToString();
list.Add(stu);
}
}
}
}
}
public class ClsSerial
{
private string path = "";
private object obj = null;
private Type type = null;
public ClsSerial()
{
}
public ClsSerial(string path, object obj)
{
this.path = path;
this.obj = obj;
}
public void SerializeNow(string path, object obj)
{
try
{
this.path = path;
this.obj = obj;
SerializeNow();
}
catch (Exception ex)
{
}
}
public void SerializeNow()
{
try
{
FileStream fileStream = new FileStream(path, FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, obj);
fileStream.Close();
}
catch (Exception ex)
{
}
}
public object DeSerializeNow(string path)
{
try
{
this.path = path;
return DeSerializeNow();
}
catch (Exception ex)
{
return null;
}
}
public object DeSerializeNow()
{
try
{
object obj = null;
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter b = new BinaryFormatter();
obj = b.Deserialize(fileStream);
fileStream.Close();
return obj;
}
catch (Exception ex)
{
return null;
}
}
}