using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace ConsoleApplication10 { class Program { static void Main(string[] args) { //建立数据连接 SqlConnection conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;"); //建立数据命令 SqlCommand cmd = conn.CreateCommand(); while (true) { Console.WriteLine("========================学生信息============================"); cmd.CommandText = "select * from student"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Console.WriteLine ("学号:" + dr["code"] + " 姓名:" + dr["name"] + " 性别:" + ((bool)dr["sex"] ? "男" : "女") + " 生日:" + (((DateTime)dr["bithday"]).ToString("yyyy年MM月dd日")) + " 分数:" + (((decimal)dr["score"]).ToString("#.##")) + ""); } } conn.Close(); Console.WriteLine("============================================================"); //提示用户操作 Console.Write("请输入您想要的操作(1=添加,2=修改,3=删除,4=结束操作):"); string caozuo = Console.ReadLine(); if (caozuo == "1") { Console.WriteLine("正在为您进行添加操作!"); Console.WriteLine("**************************添加******************************"); Console.Write("请输入您要添加的学号:"); string scode = Console.ReadLine(); Console.Write("请输入您要添加的姓名:"); string sname = Console.ReadLine(); //判断用户输入是否正确 bool ssex; while (true) { Console.Write("请输入您要添加的性别(男或女):"); string shuru = Console.ReadLine(); if (shuru == "男") { ssex = true; break; } else if (shuru == "女") { ssex = false; break; } else { Console.Clear(); Console.WriteLine("您的输入性别有误!只能输入男或者女!请重新输入!"); } } DateTime sbirthday; while (true) { Console.Write("请输入您要添加的生日:"); try { DateTime dt = Convert.ToDateTime(Console.ReadLine()); sbirthday = dt; break; } catch { Console.WriteLine("您输入的日期格式不正确,年月日请用-隔开!"); } } decimal sscore; while (true) { Console.Write("请输入您要添加的分数(0~100):"); try { decimal a = Convert.ToDecimal(Console.ReadLine()); if (a >= 0 && a <= 100) { sscore = a; break; } else { Console.WriteLine("您的输入的分数有误!请输入0~100之间的数字!"); } } catch { Console.WriteLine("您输入的分数有误!请输入数字格式!"); } } cmd.CommandText = "insert into student values('" + scode + "','" + sname + "','" + ssex + "','" + sbirthday + "'," + sscore + ")"; try { conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); Console.Clear(); Console.WriteLine("添加成功!"); } catch { Console.WriteLine("连接数据库失败!请重新连接!"); } } else if (caozuo == "2") { Console.WriteLine("正在为您进行修改操作!"); Console.WriteLine("**************************修改******************************"); bool hastu = false; while (true) { Console.Write("请您输入要修改的学号:"); string scode = Console.ReadLine(); cmd.CommandText = "select * from student where code='" + scode + "'"; conn.Open(); SqlDataReader dr1 = cmd.ExecuteReader(); if (dr1.HasRows) { hastu = true; } else { hastu = false; } conn.Close(); if (hastu) { Console.WriteLine("已查询到此学生,请进行操作!"); Console.Write("请输入您要修改的姓名:"); string sname = Console.ReadLine(); bool ssex; while (true) { Console.Write("请输入您要修改的性别(男或女):"); string shuru = Console.ReadLine(); if (shuru == "男") { ssex = true; break; } else if (shuru == "女") { ssex = false; break; } else { Console.WriteLine("您的输入性别有误!只能输入男或者女!请重新输入!"); } } DateTime sbirthday; while (true) { Console.Write("请输入您要修改的生日:"); try { DateTime dt = Convert.ToDateTime(Console.ReadLine()); sbirthday = dt; break; } catch { Console.WriteLine("您输入的日期格式不正确,年月日请用-隔开!"); } } decimal sscore; while (true) { Console.Write("请输入您要修改的分数(0~100):"); try { decimal a = Convert.ToDecimal(Console.ReadLine()); if (a >= 0 && a <= 100) { sscore = a; break; } else { Console.WriteLine("您的输入的分数有误!请输入0~100之间的数字!"); } } catch { Console.WriteLine("您输入的分数有误!请输入数字格式!"); } } cmd.CommandText = "update student set name=@xingming,sex=@xingbie,bithday=@shengri,score=@fenshu where code=@bianhao"; cmd.Parameters.Clear(); //防字符串攻击 cmd.Parameters.Add("@xingming", sname); cmd.Parameters.Add("@xingbie", ssex); cmd.Parameters.Add("@shengri", sbirthday); cmd.Parameters.Add("@fenshu", sscore); cmd.Parameters.Add("@bianhao", scode); try { conn.Open(); cmd.ExecuteNonQuery(); Console.Clear(); Console.WriteLine("修改成功!"); conn.Close(); break; } catch { conn.Close(); Console.WriteLine("连接数据库失败!请重新连接!"); } } else { Console.Clear(); Console.WriteLine("查无此学生信息!请重新输入!"); } } } else if (caozuo == "3") { Console.WriteLine("正在为您进行删除操作!"); Console.WriteLine("**************************删除******************************"); bool hastu = false; while (true) { Console.Write("请您输入要删除的学生编号:"); string scode = Console.ReadLine(); cmd.CommandText = "select * from student where code='" + scode + "'"; conn.Open(); SqlDataReader dr1 = cmd.ExecuteReader(); if (dr1.HasRows) { hastu = true; } else { hastu = false; } conn.Close(); if (hastu) { Console.WriteLine("已经查询到该学生信息!正在进行删除操作!"); cmd.CommandText = "delete from student where code='" + scode + "'"; try { conn.Open(); cmd.ExecuteNonQuery(); Console.Clear(); Console.WriteLine("删除成功!"); conn.Close(); break; } catch { conn.Close(); Console.WriteLine("连接数据库失败!请重新连接!"); } } else { Console.WriteLine("查询不到此学生信息!请重新输入!"); } } } else if (caozuo == "4") { Console.WriteLine("退出操作!请摁回车键!"); break; } else { Console.WriteLine("您的输入有误!请重新输入"); } } Console.ReadLine(); } } }