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) { //1、查询全部数据显示 //创建数据连接类 SqlConnection conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); //创建数据操作类,并且关联连接类 SqlCommand cmd = conn.CreateCommand(); //编写TSQL语句 cmd.CommandText = "select *from student"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { int age = DateTime.Now.Year - Convert.ToDateTime(dr["birthday"]).Year; Console.WriteLine(dr["Code"] + " " + dr["Name"] + " " + dr["Score"] + " " + dr["birthday"] + " " + age); } } conn.Close(); //2、删除执行 Console.Write("请输入要删除的学生编号:"); string Scode = Console.ReadLine(); cmd.CommandText = "delete from student where code =‘" + Scode + "‘"; conn.Open(); cmd.ExecuteNonQuery(); Console.Clear(); Console.WriteLine("删除成功!"); conn.Close(); //3、刷新显示 cmd.CommandText = "select *from student"; conn.Open(); SqlDataReader dr1 = cmd.ExecuteReader(); if (dr1.HasRows) { while (dr1.Read()) { Console.WriteLine(dr1["Code"] + " " + dr1["Name"] + " " + dr1["Score"]); } } conn.Close(); Console.ReadLine(); } } }
时间: 2024-11-05 14:45:40