zoukankan      html  css  js  c++  java
  • 完整的修改和删除

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    namespace ado练习题
    {
        class Program
        {
            static void Main(string[] args)
            {
                SqlConnection conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;");
                SqlCommand cmd = conn.CreateCommand();
    
                while (true)
                {
                    //1、查询显示
                    cmd.CommandText = "select *from student";
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        Console.WriteLine("=================学生信息展示==================");
                        while (dr.Read())
                        {
                            int age = DateTime.Now.Year - Convert.ToDateTime(dr["birthday"]).Year;
                            Console.WriteLine(dr["Code"] + "  " + dr["Name"] + "  " + (Convert.ToBoolean(dr["Sex"]) ? "" : "") + "  " + (Convert.ToDecimal(dr["Score"]).ToString("#.##")) + "  " + (Convert.ToDateTime(dr["birthday"]).ToString("yyyy年MM月dd日")) + "  " + age);
                        }
                    }
    
                    conn.Close();
    
                    //2、请输入你想要做的操作(1:添加,2:删除,3:修改):
                    Console.Write("请输入你的操作(1=添加,2=修改,3=删除):");
                    string usesss = Console.ReadLine();
                    if (usesss == "1")
                    {
                        Console.Clear();
                        Console.WriteLine("可以添加!");
                    }
                    else if (usesss == "3")
                    {
                        Console.Clear();
                        Console.WriteLine("可以删除!");
                    }
                    else if (usesss == "2")
                    {
                        //3、提示用户操作是否成功,刷新数据,回到2等待用户操作
                        //========================================================================
                        bool HasStu = false;
    
                        while (true)
                        {
                            //1、接收用户输入进来的学生编号
                            Console.Write("请输入要修改的学生编号:");
                            string Scode = Console.ReadLine();
    
                            //2、判断有无此学生
                            cmd.CommandText = "select *from Student where code ='" + Scode + "'";
                            conn.Open();
                            SqlDataReader dr1 = cmd.ExecuteReader();
                            //3、有此学生,那么继续修改操作,如果没有,提示无此学生信心,无法修改
                            if (dr1.HasRows)
                            {
                                HasStu = true;
                            }
                            else
                            {
                                HasStu = false;
                            }
                            conn.Close();
    
                            if (HasStu)
                            {
                                Console.WriteLine("已查询到此学生信息,请进行修改:");
                                Console.Write("请输入更改后的学生姓名:");
                                string Sname = Console.ReadLine();
                                Console.Write("请输入更改后的学生性别:");
                                bool Ssex = Convert.ToBoolean(Console.ReadLine());
                                Console.Write("请输入更改后的学生生日:");
                                DateTime Sbirthday = Convert.ToDateTime(Console.ReadLine());
                                Console.Write("请输入更改后的学生成绩:");
                                decimal Sscore = Convert.ToDecimal(Console.ReadLine());
    
                                cmd.CommandText = "update student set name=@Sname,sex=@Ssex,birthday=@Sbirthday,score=@Sscore where code = @Scode";
                                cmd.Parameters.Clear();
                                cmd.Parameters.Add("@Sname", Sname);
                                cmd.Parameters.Add("@Ssex", Ssex);
                                cmd.Parameters.Add("@Sbirthday", Sbirthday);
                                cmd.Parameters.Add("@Sscore", Sscore);
                                cmd.Parameters.Add("@Scode", Scode);
    
                                conn.Open();
                                cmd.ExecuteNonQuery();
                                Console.Clear();
                                Console.WriteLine("修改成功!");
                                conn.Close();
                                break;
                            }
                            else
                            {
                                Console.WriteLine("查无此学生,请重新输入!按任意键继续...");
                                Console.ReadKey();
                            }
                        }
    
                        //=========================================================================
                    }
                    else
                    {
                        Console.WriteLine("输入有误!请重新输入!");
                    }
    
                }
    
    
    
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    大数据基础---Hbase搭建
    大数据基础---Hbase是什么?
    大数据基础---Hive的搭建
    hive_异常_01_ Terminal initialization failed; falling back to unsupported
    Exception in thread "main" java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bsystem:user.name%7D
    Jupyter notebook 平台字体修改
    Git教程04——GitHub远端仓库内容同步本地仓库
    Git教程03——本地仓库内容同步到GitHub远程仓库
    Git教程02——利用Git GUI 连接 GitHub远程仓库
    Git教程01——Windows 安装 Git
  • 原文地址:https://www.cnblogs.com/zhangdemin/p/5617528.html
Copyright © 2011-2022 走看看