ADO.net 增删改查小练习
Posted 马MZJ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ADO.net 增删改查小练习相关的知识,希望对你有一定的参考价值。
练习题:
1、查询显示
2、请输入你想要做的操作(1:添加,2:删除,3:修改):
3、提示用户操作是否成功,刷新数据,回到2等待用户操作
建表:
1 create database ADOlianxi0425 2 go 3 use ADOlianxi0425 4 go 5 create table student 6 ( 7 code int primary key identity(100,1), 8 name nvarchar(50), 9 sex char(5), 10 birthday datetime, 11 score decimal(18,2) 12 ) 13 go 14 insert into student values(‘张三‘,‘男‘,‘1993-2-5‘,89.5) 15 insert into student values(‘张四‘,‘男‘,‘1994-8-13‘,80) 16 insert into student values(‘张五‘,‘女‘,‘1995-6-20‘,99) 17 insert into student values(‘张六‘,‘男‘,‘1996-11-11‘,62) 18 insert into student values(‘张七‘,‘男‘,‘1997-4-9‘,69) 19 go
操作:
1 SqlConnection a = new SqlConnection("server=.;database=ADOlianxi0425;user=sa;pwd=123;"); 2 SqlCommand b = a.CreateCommand(); 3 b.CommandText = "select * from student"; 4 a.Open(); 5 SqlDataReader c = b.ExecuteReader();//显示表的全部数据 6 if (c.HasRows) 7 { 8 while (c.Read()) 9 { 10 Console.WriteLine("学号:" + c["code"] + ",姓名:" + c["name"] + ",性别:" + c["sex"] + ",生日:" + ((DateTime)c["birthday"] ).ToString("yyyy年MM月dd日") + ",成绩" + ((decimal)c["score"]).ToString("#.##")); 11 } 12 } 13 a.Close(); 14 for (; ; ) 15 { 16 Console.Write("请输入添加/删除/修改/结束:"); 17 string ML = Console.ReadLine(); 18 if (ML == "添加")//添加数据 19 { 20 for (; ; ) 21 { 22 try 23 { 24 Console.Write("请输入要添加的学生姓名:"); 25 string name = Console.ReadLine(); 26 Console.Write("请输入要添加的学生性别:"); 27 string sex = Console.ReadLine(); 28 Console.Write("请输入要添加的学生生日:"); 29 DateTime birthday = Convert.ToDateTime(Console.ReadLine()); 30 Console.Write("请输入要添加的学生成绩:"); 31 decimal score = Convert.ToDecimal(Console.ReadLine()); 32 b.CommandText = "insert into student values(‘" + name + "‘,‘" + sex + "‘,‘" + birthday + "‘,‘" + score + "‘)"; 33 a.Open(); 34 b.ExecuteNonQuery(); 35 a.Close(); 36 break; 37 } 38 catch 39 { 40 Console.WriteLine("输入错误请重新输入!"); 41 } 42 } 43 } 44 else if (ML == "删除")//删除数据 45 { 46 for (; ; ) 47 { 48 try 49 { 50 Console.Write("请输入要删除的学生学号:"); 51 int code = int.Parse(Console.ReadLine()); 52 b.CommandText = "delete from student where code = " + code; 53 a.Open(); 54 b.ExecuteNonQuery(); 55 a.Close(); 56 break; 57 } 58 catch 59 { 60 Console.WriteLine("输入错误请重新输入!"); 61 } 62 } 63 } 64 else if (ML == "修改")//修改数据 65 { 66 for (; ; ) 67 { 68 try 69 { 70 Console.Write("请输入要修改的学生的学号:"); 71 int code = int.Parse(Console.ReadLine()); 72 Console.Write("请输入修改后的学生姓名:"); 73 string name = Console.ReadLine(); 74 Console.Write("请输入修改后的学生性别:"); 75 string sex = Console.ReadLine(); 76 Console.Write("请输入修改后的学生生日:"); 77 DateTime birthday = Convert.ToDateTime(Console.ReadLine()); 78 Console.Write("请输入修改后的学生成绩:"); 79 decimal score = Convert.ToDecimal(Console.ReadLine()); 80 b.CommandText = "update student set name = ‘" + name + "‘,sex = ‘" + sex + "‘,birthday = ‘" + birthday + "‘,score = " + score + "where code = " + code; 81 a.Open(); 82 b.ExecuteNonQuery(); 83 a.Close(); 84 break; 85 } 86 catch 87 { 88 Console.WriteLine("输入错误请重新输入!"); 89 } 90 } 91 } 92 else if (ML == "结束")//结束运行 93 { 94 Console.Clear(); 95 Console.WriteLine("----------------------------------谢谢使用!-----------------------------------"); 96 break; 97 } 98 else 99 { 100 Console.WriteLine("您的输入有误请重新输入!"); 101 } 102 if (ML == "添加" || ML == "修改" || ML == "删除")//如果输入错误不执行下面的代码 103 { 104 Console.Clear(); 105 b.CommandText = "select * from student"; 106 a.Open(); 107 SqlDataReader c1 = b.ExecuteReader();//显示修改后表的全部数据 108 if (c1.HasRows) 109 { 110 while (c1.Read()) 111 { 112 Console.WriteLine("学号:" + c1["code"] + ",姓名:" + c1["name"] + ",性别:" + c1["sex"] + ",生日:" + ((DateTime)c1 ["birthday"]).ToString("yyyy年MM月dd日") + ",成绩" + ((decimal)c1["score"]).ToString("#.##")); 113 } 114 } 115 a.Close(); 116 if (ML == "添加") 117 { 118 Console.WriteLine("添加成功!"); 119 } 120 else if (ML == "修改") 121 { 122 Console.WriteLine("修改成功!"); 123 } 124 else if (ML == "删除") 125 { 126 Console.WriteLine("删除成功!"); 127 } 128 } 129 }
以上是关于ADO.net 增删改查小练习的主要内容,如果未能解决你的问题,请参考以下文章