C#控制台应用程序之旅游资源与线路管理系统

利用C#语言及SQL Server数据库编写的一个简化版旅游资源与线路管理系统

数据库中包含三张表:hotel表、tourist_spot表、lines表

用户分为管理员和普通用户,管理员拥有最高权限,可以对三张表进行增、删、改、查,而普通用户拥有部分权限,只能进行查询操作

  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;
  7 using System.Data.SqlClient;
  8 
  9 namespace ConsoleApp1
 10 {
 11     class Program
 12     {
 13         static string[] Users = new string[] { "a123", "abcd","admin","driver" };
 14         static void Main(string[] args)
 15         {
 16             string str_id = "",str_password="";
 17             Console.WriteLine("      ********************************");
 18             Console.WriteLine("  ****欢迎您使用旅游资源及线路管理系统****");
 19             Console.WriteLine("      ********************************
");
 20             Console.WriteLine("请你验证身份,以便于我们为您提供更加优质的服务!");
 21             Console.Write("请输入您的账号:");
 22             str_id = Console.ReadLine();
 23             Console.Write("请输入您的密码:");
 24             str_password = Console.ReadLine();
 25             if (str_id == "root" && str_password == "root")
 26             {
 27                 do
 28                 {
 29                     RootMenu();
 30                     Console.WriteLine("请选择:1、继续  2、退出");
 31                 } while (Console.ReadLine() == "1");
 32                 Environment.Exit(0);
 33             }
 34             else if(isUser(str_id.Trim()))
 35             {
 36                 if(str_password.Trim()=="admin")
 37                     do
 38                     {
 39                         User();
 40                         Console.WriteLine("请选择:1、继续  2、退出");
 41                     } while (Console.ReadLine() == "1");
 42                 Environment.Exit(0);
 43             }
 44         }
 45 
 46         //判断是否为普通用户
 47         static bool isUser(string x)
 48         {
 49             foreach (string i in Users)
 50                 if (x == i)
 51                     return true;
 52                 else
 53                     continue;
 54             return false;
 55         }
 56 
 57         //root管理菜单
 58         static void RootMenu()
 59         {
 60             Console.Clear();
 61             Console.WriteLine("您是超级管理员,拥有最高权限");
 62             Console.WriteLine("      1、酒店信息管理");
 63             Console.WriteLine("      2、景点信息管理");
 64             Console.WriteLine("      3、线路信息管理");
 65             bool bool1 = true;
 66             do
 67             {
 68                 Console.Write("请输入序号选择你要管理的内容:");
 69                 string str = Console.ReadLine();
 70                 switch (str)
 71                 {
 72                     case "1":
 73                         Console.Clear();
 74                         Console.WriteLine("欢迎进行酒店信息管理");
 75                         Console.WriteLine("1、酒店信息添加");
 76                         Console.WriteLine("2、酒店信息删除");
 77                         Console.WriteLine("3、酒店信息修改");
 78                         Console.WriteLine("4、酒店信息查询");
 79                         Root(1);
 80                         break;
 81                     case "2":
 82                         Console.Clear();
 83                         Console.WriteLine("欢迎进行景点信息管理");
 84                         Console.WriteLine("1、景点信息添加");
 85                         Console.WriteLine("2、景点信息删除");
 86                         Console.WriteLine("3、景点信息修改");
 87                         Console.WriteLine("4、景点信息查询");
 88                         Root(2);
 89                         break; ;
 90                     case "3":
 91                         Console.Clear();
 92                         Console.WriteLine("欢迎进行线路信息管理");
 93                         Console.WriteLine("1、线路信息添加");
 94                         Console.WriteLine("2、线路信息删除");
 95                         Console.WriteLine("3、线路信息修改");
 96                         Console.WriteLine("4、线路信息查询");
 97                         Root(3);
 98                         break;
 99                     default:
100                         bool1 = false;
101                         break;
102                 }
103             } while (!bool1);
104         }
105 
106         //root函数,具有所有数据操作的权限
107         static void Root(int x)
108         {
109             switch (x)
110             {
111                 case 1:
112                     {
113                         Console.Write("请选择:");
114                         string str = "";
115                         str = Console.ReadLine();
116                         switch (str.Trim())
117                         {
118                             case "1":
119                                 HotelAdd();
120                                 break;
121                             case "2":
122                                 HotelDelete();
123                                 break;
124                             case "3":
125                                 HotelAlter();
126                                 break;
127                             case "4":
128                                 HotelSelect();
129                                 break;
130                             default:
131                                 Console.Write("非法操作!");
132                                 return;
133                         }
134                     }
135                     break;
136 
137                 case 2:
138                     {
139                         Console.Write("请选择:");
140                         string str = "";
141                         str = Console.ReadLine();
142                         switch (str.Trim())
143                         {
144                             case "1":
145                                 Tourist_spotAdd();
146                                 break;
147                             case "2":
148                                 Tourist_spotDelete();
149                                 break;
150                             case "3":
151                                 Tourist_spotAlter();
152                                 break;
153                             case "4":
154                                 Tourist_spotSelect();
155                                 break;
156                             default:
157                                 Console.Write("非法操作!");
158                                 return;
159                         }
160                     }
161                     break;
162                 case 3:
163                     {
164                         Console.Write("请选择:");
165                         string str = "";
166                         str = Console.ReadLine();
167                         switch (str.Trim())
168                         {
169                             case "1":
170                                 LinesAdd();
171                                 break;
172                             case "2":
173                                 LinesDelete();
174                                 break;
175                             case "3":
176                                 LinesAlter();
177                                 break;
178                             case "4":
179                                 LinesSelect();
180                                 break;
181                             default:
182                                 Console.Write("非法操作!");
183                                 return;
184                         }
185                     }
186                     break;
187                 default:
188                     {
189                         Console.Write("非法操作!");
190                         return;
191                     }
192             }
193         }
194 
195 
196         //user函数,具有查询功能
197         static void User()
198         {
199             Console.Clear();
200             Console.WriteLine("您是普通用户,拥有查询权限");
201             Console.WriteLine("      1、查询酒店信息");
202             Console.WriteLine("      2、查询景点信息");
203             Console.WriteLine("      3、查询线路信息");
204             Console.Write("请输入序号选择您要查询的内容:");
205             string str = Console.ReadLine();
206             switch(str)
207             {
208                 case "1":
209                     HotelSelect();
210                     break;
211                 case "2":
212                     Tourist_spotSelect();
213                     break;
214                 case "3":
215                     LinesSelect();
216                     break;
217                 default:
218                     Console.WriteLine("非法输入!");
219                     return;
220             }
221         }
222 
223         //hotel信息管理函数
224         #region
225         //hotel信息添加
226         static void HotelAdd()
227         {
228             Console.WriteLine("请输入你要添加的酒店信息");
229             Console.Write("酒店名称:");
230             string str_hotel_name = Console.ReadLine() ;
231             Console.Write("酒店地址:");
232             string str_hotel_addr = Console.ReadLine();
233             Console.Write("酒店电话:");
234             string str_hotel_tel = Console.ReadLine();
235             Console.Write("酒店价格:");
236             int int_hotel_price = Convert.ToInt32(Console.ReadLine());
237             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
238                 "Integrated Security = True;";
239             SqlConnection sqlCon = new SqlConnection(StrCon);
240                 sqlCon.Open();
241                 string isStr = @"insert into hotel values('" + str_hotel_name +"','" + str_hotel_addr + "'," 
242                     + str_hotel_tel + ","+int_hotel_price+")";
243                 SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
244                 sqlCmd.ExecuteNonQuery();
245                 Console.WriteLine("酒店信息添加成功!");
246             sqlCon.Close();
247         }
248 
249         //hotel信息删除
250         static void HotelDelete()
251         {
252             Console.WriteLine("请输入你要删除的酒店信息");
253             Console.Write("酒店名称:");
254             string str_hotel_name = Console.ReadLine();
255             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
256                 "Integrated Security = True;";
257             SqlConnection sqlCon = new SqlConnection(StrCon);
258             sqlCon.Open();
259             string isStr = @"delete from hotel where hotel_name ='" + str_hotel_name + "'";
260             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
261             sqlCmd.ExecuteNonQuery();
262             Console.WriteLine("酒店信息删除成功!");
263             sqlCon.Close();
264         }
265 
266         //hotel信息修改
267         static void HotelAlter()
268         {
269             Console.WriteLine("请输入要修改的酒店名称:");
270             Console.Write("酒店名称:");
271             string str_hotel_name_before = Console.ReadLine();
272             Console.WriteLine("请输入修改后的酒店信息");
273             Console.Write("酒店名称:");
274             string str_hotel_name = Console.ReadLine();
275             Console.Write("酒店地址:");
276             string str_hotel_addr = Console.ReadLine();
277             Console.Write("酒店电话:");
278             string str_hotel_tel = Console.ReadLine();
279             Console.Write("酒店价格:");
280             int int_hotel_price = Convert.ToInt32(Console.ReadLine());
281             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
282                 "Integrated Security = True;";
283             SqlConnection sqlCon = new SqlConnection(StrCon);
284             sqlCon.Open();
285             string isStr = @"update hotel set  hotel_name ='" + str_hotel_name +
286                 "',hotel_addr='" + str_hotel_addr + "',hotel_tel ='" + str_hotel_tel
287                 + "',hotel_price=" + int_hotel_price + " where hotel_name='" + str_hotel_name_before + "'";
288             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
289             sqlCmd.ExecuteNonQuery();
290             Console.WriteLine("酒店信息修改成功!");
291             sqlCon.Close();
292         }
293 
294         //hotel信息查询
295         static void HotelSelect()
296         {
297             Console.WriteLine("请输入你要查询的酒店地址");
298             Console.Write("酒店地址:");
299             string str_hotel_addr = Console.ReadLine();
300             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
301                 "Integrated Security = True;";
302             SqlConnection sqlCon = new SqlConnection(StrCon);
303             sqlCon.Open();
304             string isStr = @"select * from hotel where hotel_addr ='" + str_hotel_addr + "'";
305             string sltResult = "";
306             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
307             SqlDataReader reader = sqlCmd.ExecuteReader();
308             Console.WriteLine("查询结果:");
309             if (reader.HasRows)
310             {
311                 while(reader.Read())
312                 {
313                     //记录每一条记录
314                     sltResult += "名称:"+reader["hotel_name"].ToString() +"	电话:" + reader["hotel_tel"].ToString()
315                         +"	价格:"+ reader["hotel_price"].ToString() + "
";
316                 }
317                 Console.WriteLine(sltResult);
318             }
319             Console.WriteLine("酒店信息查询成功!");
320             sqlCon.Close();
321         }
322         #endregion
323 
324         //tourist_spot信息管理函数
325         #region
326         //tourist_spot信息添加
327         static void Tourist_spotAdd()
328         {
329             Console.WriteLine("请输入你要添加的景点信息");
330             Console.Write("景点名称:");
331             string str_Tourist_spot_name = Console.ReadLine();
332             Console.Write("景点地址:");
333             string str_Tourist_spot_addr = Console.ReadLine();
334             Console.Write("景点价格:");
335             int int_Tourist_spot_price = Convert.ToInt32(Console.ReadLine());
336             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
337                 "Integrated Security = True;";
338             SqlConnection sqlCon = new SqlConnection(StrCon);
339             sqlCon.Open();
340             string isStr = @"insert into tourist_spot values('" + str_Tourist_spot_name + "','" + str_Tourist_spot_addr + 
341                 "'," + int_Tourist_spot_price + ")";
342             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
343             sqlCmd.ExecuteNonQuery();
344             Console.WriteLine("酒店信息添加成功!");
345             sqlCon.Close();
346         }
347 
348         //tourist_spot信息删除
349         static void Tourist_spotDelete()
350         {
351             Console.WriteLine("请输入你要删除的景点信息");
352             Console.Write("景点名称:");
353             string str_tourist_spot_name = Console.ReadLine();
354             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
355                 "Integrated Security = True;";
356             SqlConnection sqlCon = new SqlConnection(StrCon);
357             sqlCon.Open();
358             string isStr = @"delete from tourist_spot where tourist_spot_name ='" + str_tourist_spot_name + "'";
359             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
360             sqlCmd.ExecuteNonQuery();
361             Console.WriteLine("景点信息删除成功!");
362             sqlCon.Close();
363         }
364 
365         //tourist_spot信息修改
366         static void Tourist_spotAlter()
367         {
368             Console.WriteLine("请输入要修改的景点名称:");
369             Console.Write("景点名称:");
370             string str_Tourist_spot_name_before = Console.ReadLine();
371             Console.WriteLine("请输入修改后的景点信息");
372             Console.Write("景点名称:");
373             string str_Tourist_spot_name = Console.ReadLine();
374             Console.Write("景点地址:");
375             string str_Tourist_spot_addr = Console.ReadLine();
376             Console.Write("景点价格:");
377             int int_Tourist_spot_price = Convert.ToInt32(Console.ReadLine());
378             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
379                 "Integrated Security = True;";
380             SqlConnection sqlCon = new SqlConnection(StrCon);
381             sqlCon.Open();
382             string isStr = @"update tourist_spot set  tourist_spot_name ='" + str_Tourist_spot_name +
383                 "',tourist_spot_addr='" + str_Tourist_spot_addr + "',tourist_spot_price=" +
384                 int_Tourist_spot_price + " where tourist_spot_name='" + str_Tourist_spot_name_before + "'";
385             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
386             sqlCmd.ExecuteNonQuery();
387             Console.WriteLine("景点信息修改成功!");
388             sqlCon.Close();
389         }
390 
391         //tourist_spot信息查询
392         static void Tourist_spotSelect()
393         {
394             Console.Write("请输入你要查询的景点地址:");
395             string str_Tourist_spot_addr = Console.ReadLine();
396             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
397                 "Integrated Security = True;";
398             SqlConnection sqlCon = new SqlConnection(StrCon);
399             sqlCon.Open();
400             string isStr = @"select * from tourist_spot where tourist_spot_addr ='" + str_Tourist_spot_addr + "'";
401             string sltResult = "";
402             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
403             SqlDataReader reader = sqlCmd.ExecuteReader();
404             Console.WriteLine("查询结果:");
405             if (reader.HasRows)
406             {
407                 while (reader.Read())
408                 {
409                     //记录每一条记录
410                     sltResult += "名称:" + reader["tourist_spot_name"].ToString() 
411                         + "	价格:" + reader["tourist_spot_price"].ToString() + "
";
412                 }
413                 Console.WriteLine(sltResult);
414             }
415             Console.WriteLine("景点信息查询成功!");
416             sqlCon.Close();
417         }
418         #endregion
419 
420         //lines信息管理函数
421         #region
422         //lines信息添加
423         static void LinesAdd()
424         {
425             Console.WriteLine("请输入你要添加的线路信息");
426             Console.Write("车次号码:");
427             string str_lines_num = Console.ReadLine();
428             Console.Write("发车时间:");
429             string str_lines_time = Console.ReadLine();
430             Console.Write("火车起点:");
431             string str_lines_from = Console.ReadLine();
432             Console.Write("火车终点:");
433             string str_lines_to = Console.ReadLine();
434             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
435                 "Integrated Security = True;";
436             SqlConnection sqlCon = new SqlConnection(StrCon);
437             sqlCon.Open();
438             string isStr = @"insert into lines values('" + str_lines_num + "','" + str_lines_time + "','" + 
439                 str_lines_from + "','" + str_lines_to + "')";
440             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
441             sqlCmd.ExecuteNonQuery();
442             Console.WriteLine("线路信息添加成功!");
443             sqlCon.Close();
444         }
445 
446         //lines信息删除
447         static void LinesDelete()
448         {
449             Console.WriteLine("请输入你要删除的线路信息");
450             Console.Write("车次号码:");
451             string str_lines_num = Console.ReadLine();
452             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
453                 "Integrated Security = True;";
454             SqlConnection sqlCon = new SqlConnection(StrCon);
455             sqlCon.Open();
456             string isStr = @"delete from lines where lines_num =" + str_lines_num ;
457             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
458             sqlCmd.ExecuteNonQuery();
459             Console.WriteLine("线路信息删除成功!");
460             sqlCon.Close();
461         }
462 
463         //lines信息修改
464         static void LinesAlter()
465         {
466             Console.WriteLine("请输入你要修改的线路信息");
467             Console.Write("车次号码:");
468             string str_lines_num_before = Console.ReadLine();
469             Console.WriteLine("请输入修改后的线路信息");
470             Console.Write("车次号码:");
471             string str_lines_num = Console.ReadLine();
472             Console.Write("发车时间:");
473             string str_lines_time = Console.ReadLine();
474             Console.Write("火车起点:");
475             string str_lines_from = Console.ReadLine();
476             Console.Write("火车终点:");
477             string str_lines_to = Console.ReadLine();
478             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
479                 "Integrated Security = True;";
480             SqlConnection sqlCon = new SqlConnection(StrCon);
481             sqlCon.Open();
482             string isStr = @"update lines set  lines_num ='" + str_lines_num + "',lines_time='" +
483                 str_lines_time + "',lines_from='" + str_lines_from + "',lines_to='" + str_lines_to
484                 + "' where lines_num=" + str_lines_num_before;
485             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
486             sqlCmd.ExecuteNonQuery();
487             Console.WriteLine("线路信息修改成功!");
488             sqlCon.Close();
489         }
490 
491         //lines信息查询
492         static void LinesSelect()
493         {
494             Console.Write("请输入你要查询的车次号码:");
495             string str_lines_num = Console.ReadLine();
496             string StrCon = @"Data Source = .;Initial Catalog=tourResour_lines;" +
497                 "Integrated Security = True;";
498             SqlConnection sqlCon = new SqlConnection(StrCon);
499             sqlCon.Open();
500             string isStr = @"select * from lines where lines_num =" + str_lines_num ;
501             string sltResult = "";
502             SqlCommand sqlCmd = new SqlCommand(isStr, sqlCon);
503             SqlDataReader reader = sqlCmd.ExecuteReader();
504             Console.WriteLine("查询结果:");
505             if (reader.HasRows) 
506             {
507                 while (reader.Read())
508                 {
509                     //记录每一条记录
510                     sltResult += "车次号码:" + reader["lines_num"].ToString() + "	发车时间:" +
511                         reader["lines_time"].ToString() + "	起始地点:" + reader["lines_from"].ToString() +
512                         "	终点城市:"+reader["lines_to"].ToString() + "
";
513                 }
514                 Console.WriteLine(sltResult);
515             }
516             Console.WriteLine("线路信息查询成功!");
517             sqlCon.Close();
518         }
519         #endregion
520     }
521 }

作者:耑新新,发布于  博客园

转载请注明出处,欢迎邮件交流:zhuanxinxin@aliyun.com

原文地址:https://www.cnblogs.com/Arthurian/p/7138348.html