zoukankan      html  css  js  c++  java
  • List和模型学完后的练习

    概述:控制台程序,sqlserver数据库,库D1,表T1。

    学生信息表:

    表结构:

    其中id自动编号。

    主程序先完成框架,循环录入选项进行操作:

     1 namespace ConsoleApplication1
     2 {
     3     class Program
     4     {
     5         static void Select_all()
     6         {
     7             Console.WriteLine("Select_all");
     8         }
     9         static void Select_one()
    10         {
    11             Console.WriteLine("Select_one");
    12         }
    13         static void Add_one()
    14         {
    15             Console.WriteLine("Add_one");
    16         }
    17         static void Delete_one()
    18         {
    19             Console.WriteLine("Delete_one");
    20         }
    21         static void Modify_one()
    22         {
    23             Console.WriteLine("Modify_one");
    24         }
    25         static void Main(string[] args)
    26         {
    27             int a;
    28             do
    29             {
    30                 Console.WriteLine("请输入你要执行的操作:
    1、显示全部学生
    2、显示单个学生
    3、添加新学生
    4、删除学生数据
    5、修改学生数据
    0、退出!");
    31                 a = int.Parse(Console.ReadLine());
    32                 switch(a)
    33                 {
    34                     case 1:
    35                         Select_all();
    36                         break;
    37                     case 2:
    38                         Select_one();
    39                         break;
    40                     case 3:
    41                         Add_one();
    42                         break;
    43                     case 4:
    44                         Delete_one();
    45                         break;
    46                     case 5:
    47                         Modify_one();
    48                         break;
    49                     default:
    50                         Console.WriteLine("输入有误!");
    51                         break;
    52 
    53                 }
    54             }
    55             while (a != 0);
    56         }
    57     }
    58 }

    测试效果如下:

     为项目添加EF框架(过程略,可参见相关博文)

    结果如下:

     添加业务逻辑层类:

     代码如下:

     1 namespace ConsoleApplication1.Bll
     2 {
     3     class Student
     4     {
     5         public static List<t1> get_All()
     6         {
     7             var d1=new D1();
     8             return d1.t1.ToList();
     9         }
    10         public static t1 get_One(int id)
    11         {
    12             var d1 = new D1();
    13             return d1.t1.Where(x => x.id == id).FirstOrDefault();
    14         }
    15         public static void add_One(t1 x)
    16         {
    17             var d1 = new D1();
    18             d1.t1.Add(x);
    19             d1.SaveChanges();
    20         }
    21         public static void del_One(int x)
    22         {
    23             var d1 = new D1();
    24             var a = d1.t1.Where(t => t.id == x).FirstOrDefault();
    25             if(a!=null)
    26             {
    27                 d1.t1.Remove(a);
    28                 d1.SaveChanges();
    29             }
    30         }
    31         public static void modi_One(t1 x)
    32         {
    33             var d1 = new D1();
    34             var t = d1.t1.Where(xx=>xx.id==x.id).First();
    35             if(t!=null)
    36             {
    37                 t.xm = x.xm;
    38                 t.nl = x.nl;
    39             }
    40             d1.SaveChanges();
    41         }
    42     }
    43 }

    修改完善主程序,代码如下:

     1 class Program
     2     {
     3         static void Select_all()
     4         {
     5             //Console.WriteLine("Select_all");
     6             List<t1> a = Student.get_All();
     7             Console.Write("学号	姓名	年龄
    ");
     8             foreach (var item in a)
     9             {
    10                 Console.Write(item.id + "	" + item.xm + "	" + item.nl+"
    ");
    11             }
    12         }
    13         static void Select_one()
    14         {
    15             //Console.WriteLine("Select_one");
    16             int no;
    17             Console.WriteLine("输入要查询的学生学号:");
    18             no=int.Parse(Console.ReadLine());
    19             t1 a = Student.get_One(no);
    20             if(a!=null)
    21             {
    22                 Console.Write("学号	姓名	年龄
    ");
    23                 Console.Write(a.id + "	" + a.xm + "	" + a.nl + "
    ");
    24             }
    25             else
    26             {
    27                 Console.WriteLine("查无此人");
    28             }
    29         }
    30         static void Add_one()
    31         {
    32             //Console.WriteLine("Add_one");
    33             t1 a = new t1();
    34             Console.WriteLine("输入要添加的学生姓名:");
    35             a.xm=Console.ReadLine();
    36             Console.WriteLine("输入要添加的学生年龄:");
    37             a.nl=short.Parse(Console.ReadLine());
    38             Student.add_One(a);
    39         }
    40         static void Delete_one()
    41         {
    42             //Console.WriteLine("Delete_one");
    43             int a;
    44             Console.WriteLine("输入要删除的学生学号:");
    45             a = int.Parse(Console.ReadLine());
    46             Student.del_One(a);
    47         }
    48         static void Modify_one()
    49         {
    50             //Console.WriteLine("Modify_one");
    51             t1 a = new t1();
    52             Console.WriteLine("输入要修改的学生学号:");
    53             a.id = int.Parse(Console.ReadLine());
    54             Console.WriteLine("输入要修改的学生姓名:");
    55             a.xm = Console.ReadLine();
    56             Console.WriteLine("输入要修改的学生年龄:");
    57             a.nl = short.Parse(Console.ReadLine());
    58             Student.modi_One(a);
    59         }
    60         static void Main(string[] args)
    61         {
    62             int a;
    63             do
    64             {
    65                 Console.WriteLine("请输入你要执行的操作:
    1、显示全部学生
    2、显示单个学生
    3、添加新学生
    4、删除学生数据
    5、修改学生数据
    0、退出!");
    66                 a = int.Parse(Console.ReadLine());
    67                 switch(a)
    68                 {
    69                     case 0:
    70                         break;
    71                     case 1:
    72                         Select_all();
    73                         break;
    74                     case 2:
    75                         Select_one();
    76                         break;
    77                     case 3:
    78                         Add_one();
    79                         break;
    80                     case 4:
    81                         Delete_one();
    82                         break;
    83                     case 5:
    84                         Modify_one();
    85                         break;
    86                     default:
    87                         Console.WriteLine("输入有误!");
    88                         break;
    89 
    90                 }
    91             }
    92             while (a != 0);
    93         }
    94     }

    至此,程序工作正常。可自行调试。

  • 相关阅读:
    打开Intellij Idea 2020.1 提示 cannot load a jdk class: com.sun.jdi.Field
    win10触摸板设置为连接鼠标不打开后就自动关闭
    git配置账号
    HTTP请求中的Form Data与Request Payload的区别
    VUE—axios自定义请求配置—3、transformRequest在向服务器发送前,修改请求数据(图文详情)
    在Sass中,我们可以使用“@for”来实现循环操作
    vue项目引入背景图报Module not found: Error: Can't resolve './src/assets/theme/logo_blue.png' in'xxx'错误
    Importing code style from ESLint
    ESLint fix自动修复所有格式问题
    【T07】不要低估tcp的性能
  • 原文地址:https://www.cnblogs.com/wanjinliu/p/11791387.html
Copyright © 2011-2022 走看看