using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ado练习题 { class Program { static void Main(string[] args) { SqlConnection conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); SqlCommand cmd = conn.CreateCommand(); while (true) { //1、查询显示 cmd.CommandText = "select *from student"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { Console.WriteLine("=================学生信息展示=================="); while (dr.Read()) { int age = DateTime.Now.Year - Convert.ToDateTime(dr["birthday"]).Year; Console.WriteLine(dr["Code"] + " " + dr["Name"] + " " + (Convert.ToBoolean(dr["Sex"]) ? "男" : "女") + " " + (Convert.ToDecimal(dr["Score"]).ToString("#.##")) + " " + (Convert.ToDateTime(dr["birthday"]).ToString("yyyy年MM月dd日")) + " " + age); } } conn.Close(); //2、请输入你想要做的操作(1:添加,2:删除,3:修改): Console.Write("请输入你的操作(1=添加,2=修改,3=删除):"); string usesss = Console.ReadLine(); if (usesss == "1") { Console.Clear(); Console.WriteLine("可以添加!"); } else if (usesss == "3") { Console.Clear(); Console.WriteLine("可以删除!"); } else if (usesss == "2") { //3、提示用户操作是否成功,刷新数据,回到2等待用户操作 //======================================================================== bool HasStu = false; while (true) { //1、接收用户输入进来的学生编号 Console.Write("请输入要修改的学生编号:"); string Scode = Console.ReadLine(); //2、判断有无此学生 cmd.CommandText = "select *from Student where code =‘" + Scode + "‘"; conn.Open(); SqlDataReader dr1 = cmd.ExecuteReader(); //3、有此学生,那么继续修改操作,如果没有,提示无此学生信心,无法修改 if (dr1.HasRows) { HasStu = true; } else { HasStu = false; } conn.Close(); if (HasStu) { Console.WriteLine("已查询到此学生信息,请进行修改:"); Console.Write("请输入更改后的学生姓名:"); string Sname = Console.ReadLine(); Console.Write("请输入更改后的学生性别:"); bool Ssex = Convert.ToBoolean(Console.ReadLine()); Console.Write("请输入更改后的学生生日:"); DateTime Sbirthday = Convert.ToDateTime(Console.ReadLine()); Console.Write("请输入更改后的学生成绩:"); decimal Sscore = Convert.ToDecimal(Console.ReadLine()); cmd.CommandText = "update student set [email protected],[email protected],[email protected],[email protected] where code = @Scode"; cmd.Parameters.Clear(); cmd.Parameters.Add("@Sname", Sname); cmd.Parameters.Add("@Ssex", Ssex); cmd.Parameters.Add("@Sbirthday", Sbirthday); cmd.Parameters.Add("@Sscore", Sscore); cmd.Parameters.Add("@Scode", Scode); conn.Open(); cmd.ExecuteNonQuery(); Console.Clear(); Console.WriteLine("修改成功!"); conn.Close(); break; } else { Console.WriteLine("查无此学生,请重新输入!按任意键继续..."); Console.ReadKey(); } } //========================================================================= } else { Console.WriteLine("输入有误!请重新输入!"); } } Console.ReadKey(); } } }
时间: 2024-10-25 05:54:13