4 using System; 5 using System.Collections.Generic;using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 using System.Data.SqlClient; 9 10 namespace 练习题 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 //练习题: 17 //1、查询显示 18 #region 19 SqlConnection conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123"); 20 //↑创建数据库连接 21 SqlCommand com = conn.CreateCommand(); 22 //↑创建数据库操作 23 24 //查询全部内容 25 com.CommandText = "select * from student"; 26 //打开连接通道 27 conn.Open(); 28 SqlDataReader a1 = com.ExecuteReader(); 29 if (a1.HasRows) 30 { 31 while (a1.Read()) 32 { 33 Console.WriteLine("学号:" + a1[0] + ",姓名:" + a1["name"] + ",性别:" + (((bool)a1[2]) ? "男" : "女") + ",生日:" + Convert.ToDateTime(a1[3]).ToString("yyyy年MM月dd日") + ",成绩:" + ((decimal)a1[4]).ToString("#.##")); 34 } 35 } 36 //关闭连接通道 37 conn.Close(); 38 39 #endregion 40 41 //2、请输入你想要做的操作(1:添加,2:删除,3:修改): 42 43 //3、提示用户操作是否成功,刷新数据,回到2等待用户操作 44 45 for (; ; ) //死循环--- 回到2等待用户操作 46 { 47 Console.Write("请输入你想要做的操作的序号:(1:添加,2:删除,3:修改,4:操作完毕)"); 48 int cz = int.Parse(Console.ReadLine()); //2请输入你想要做的操作 49 50 #region //添加数据 51 if (cz == 1) 52 { 53 Console.Write("请输入学生学号:"); 54 string scode = Console.ReadLine(); 55 Console.Write("请输入学生姓名:"); 56 string sname = Console.ReadLine(); 57 Console.Write("请输入学生性别(1:男,0:女):"); 58 string ssex = Console.ReadLine(); 59 Console.Write("请输入学生生日:"); 60 DateTime sbirthday = Convert.ToDateTime(Console.ReadLine()); 61 Console.Write("请输入学生成绩:"); 62 decimal sscoer = Convert.ToDecimal(Console.ReadLine()); //↑输入添加内容 63 //↓sql语句--执行添加 64 //string xb; 65 //if (ssex == "男") 66 //{ xb = "1"; } 67 //else if(ssex=="女") 68 //{ xb = "0"; } 69 com.CommandText = "insert into student values('" + scode + "','" + sname + "','" + ssex + "','" + sbirthday + "'," + sscoer + ")"; 70 71 //操作是否成功 72 try 73 { //打开 74 conn.Open(); 75 com.ExecuteNonQuery(); 76 Console.WriteLine("添加成功!!!"); 77 } 78 catch 79 { 80 Console.WriteLine("添加失败!!!"); 81 } 82 //关闭 83 conn.Close(); 84 85 //刷新数据 86 //查询全部内容 87 com.CommandText = "select * from student"; 88 //打开连接通道 89 conn.Open(); 90 SqlDataReader tj = com.ExecuteReader(); 91 if (tj.HasRows) 92 { 93 while (tj.Read()) 94 { 95 Console.WriteLine("学号:" + tj[0] + ",姓名:" + tj["name"] + ",性别:" + (((bool)tj[2]) ? "男" : "女") + ",生日:" + Convert.ToDateTime(tj[3]).ToString("yyyy年MM月dd日") + ",成绩:" + ((decimal)tj[4]).ToString("#.##")); 96 } 97 } 98 //关闭连接通道 99 conn.Close(); 100 101 } 102 #endregion 103 #region //删除数据 104 else if (cz == 2) 105 { 106 bool HasStu = false; 107 for (; ; ) 108 { 109 Console.Write("请输入要删除的学生的学号:"); 110 string scode = Console.ReadLine(); //获取要删除的学号 111 112 com.CommandText = "select *from Student where code ='" + scode + "'"; 113 conn.Open(); 114 SqlDataReader dr1 = com.ExecuteReader(); 115 //3、有此学生,那么继续操作,如果没有,提示无此学生信息 116 if (dr1.HasRows) 117 { 118 HasStu = true; 119 } 120 else 121 { 122 HasStu = false; 123 } 124 conn.Close(); 125 if (HasStu) 126 { 127 //执行删除 128 com.CommandText = "delete from student where code='" + scode + "'"; 129 //提示用户操作是否成功 130 try 131 { 132 conn.Open(); 133 com.ExecuteNonQuery(); 134 Console.WriteLine("删除成功!!!"); 135 } 136 catch 137 { 138 Console.WriteLine("删除失败!!!"); 139 } 140 141 conn.Close(); 142 break; 143 } 144 else 145 { 146 Console.WriteLine("查无此人!请重新输入!"); 147 } 148 } 149 150 //刷新数据 151 //查询全部内容 152 com.CommandText = "select * from student"; 153 //打开连接通道 154 conn.Open(); 155 SqlDataReader sc = com.ExecuteReader(); 156 if (sc.HasRows) 157 { 158 while (sc.Read()) 159 { 160 Console.WriteLine("学号:" + sc[0] + ",姓名:" + sc["name"] + ",性别:" + (((bool)sc[2]) ? "男" : "女") + ",生日:" + Convert.ToDateTime(sc[3]).ToString("yyyy年MM月dd日") + ",成绩:" + ((decimal)sc[4]).ToString("#.##")); 161 } 162 } 163 //关闭连接通道 164 conn.Close(); 165 166 } 167 #endregion 168 #region //修改数据 169 else if (cz == 3) 170 { 171 bool HasStu = false; 172 for (; ; ) 173 { 174 Console.Write("请输入要修改学生的学号:"); 175 string scode = Console.ReadLine(); 176 177 178 //2、判断有无此学生 179 com.CommandText = "select *from Student where code ='" + scode + "'"; 180 conn.Open(); 181 SqlDataReader dr1 = com.ExecuteReader(); 182 //3、有此学生,那么继续修改操作,如果没有,提示无此学生信心,无法修改 183 if (dr1.HasRows) 184 { 185 HasStu = true; 186 } 187 else 188 { 189 HasStu = false; 190 } 191 conn.Close(); 192 193 if (HasStu) 194 { 195 Console.Write("请输入修改后的姓名:"); 196 string sname = Console.ReadLine(); 197 Console.Write("请输入修改后的性别(1:男,0:女):"); 198 string ssex = Console.ReadLine(); 199 Console.Write("请输入修改后的生日:"); 200 DateTime sbirthday = Convert.ToDateTime(Console.ReadLine()); 201 Console.Write("请输入修改后的成绩:"); 202 decimal sscore = Convert.ToDecimal(Console.ReadLine()); //↑修改内容 203 //↓执行修改 204 //string xb; 205 //if (ssex == "男") 206 //{ xb = "1"; } 207 //else if(ssex=="女") 208 //{ xb = "0"; } 209 com.CommandText = "update student set name='" + sname + "',sex='" + ssex + "',birthday='" + sbirthday + "',score=" + sscore + " where code='" + scode + "'"; 210 211 // 提示用户操作是否成功, 212 try 213 { 214 conn.Open(); 215 216 com.ExecuteNonQuery(); 217 Console.WriteLine("修改成功!!!"); 218 } 219 catch 220 { 221 Console.WriteLine("修改失败!!!"); 222 } 223 conn.Close(); 224 break; 225 } 226 else 227 { 228 Console.WriteLine("查无此人!重新输入!!"); 229 } 230 } 231 //刷新数据 232 //查询全部内容 233 com.CommandText = "select * from student"; 234 //打开连接通道 235 conn.Open(); 236 SqlDataReader yg = com.ExecuteReader(); 237 if (yg.HasRows) 238 { 239 while (yg.Read()) 240 { 241 Console.WriteLine("学号:" + yg[0] + ",姓名:" + yg["name"] + ",性别:" + (((bool)yg[2]) ? "男" : "女") + ",生日:" + Convert.ToDateTime(yg[3]).ToString("yyyy年MM月dd日") + ",成绩:" + ((decimal)yg[4]).ToString("#.##")); 242 243 } 244 } 245 //关闭连接通道 246 conn.Close(); 247 } 248 249 #endregion 250 #region //操作完成 251 else if (cz == 4) 252 { 253 break; //操作完毕,跳出循环 254 } 255 #endregion 256 #region //输入有误 257 else 258 { 259 Console.WriteLine("输入有误!重新输入!"); 260 } 261 #endregion 262 } 263 264 Console.ReadLine(); 265 } 266 } 267 }