ado.not--添加练习题

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

 

 

 

添加数据

posted @ 2016-06-27 15:48  右掱写爱  阅读(147)  评论(0编辑  收藏  举报