using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace ConsoleApplication6 { class Program { static void Main(string[] args) { //1.显示表格内容 //引用数据库命名空间 //建立数据库连接 SqlConnection conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;"); //在数据库连接基础上建立数据库操作 SqlCommand cmd = conn.CreateCommand(); //编写sql操作命令 cmd.CommandText = "select code,name,score from student"; //执行操作 //开启数据库 conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if(dr.HasRows) { while(dr.Read()) { Console.WriteLine(dr["code"]+" "+dr["name"]+" "+dr["score"]); } } //关闭数据库 conn.Close(); //2.删除s107 Console.Write("请输入需要删除的学号:"); string scode=Console.ReadLine(); //编写删除指令 cmd.CommandText = "delete from student where code='"+scode+"'"; //进行操作 try { //开启数据库 conn.Open(); //执行操作 cmd.ExecuteNonQuery(); Console.Clear(); Console.WriteLine("删除成功!"); } catch { Console.WriteLine("连接数据库失败!请重新连接"); } finally { //关闭数据库 conn.Close(); } //3.刷新并显示删除后的表格 //编写sql操作命令 cmd.CommandText = "select code,name,score 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(); } } }