zoukankan      html  css  js  c++  java
  • 操作数据库(增删改)

     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 ConsoleApplication1
     9 {
    10     class Program4
    11     {
    12         static void Main4(string[] args)
    13         {
    14             //用户输入要删除的数据主键值
    15             Console.WriteLine("请输入要删除的代号:");
    16             string code = Console.ReadLine();
    17 
    18             //判断该数据存不存在
    19             SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
    20             SqlCommand cmd = conn.CreateCommand();
    21             cmd.CommandText = "select * from Info where Code='"+code+"'";
    22             conn.Open();
    23             SqlDataReader dr = cmd.ExecuteReader();
    24             
    25 
    26             if (dr.HasRows)
    27             {
    28                 //说明该数据存在
    29                 Console.WriteLine("查到该数据,是否要执行删除操作,如果要删除请输入:1");
    30                 int sc = Convert.ToInt32(Console.ReadLine());
    31 
    32                 if (sc == 1)
    33                 {
    34                     //删除
    35                     dr.Close(); //关闭读取器
    36 
    37                     cmd.CommandText = "delete from Info where Code='"+code+"'";
    38                     cmd.ExecuteNonQuery();
    39                     Console.WriteLine("删除成功!");
    40                     
    41                 }
    42                 else
    43                 {
    44                     //不删除
    45                     dr.Read();
    46 
    47                     string sex = Convert.ToBoolean(dr[2])?"":"";
    48                     string nation = MinZu(dr[3].ToString());
    49 
    50                     string str = "代号:"+dr[0]+"	姓名:"+dr[1]+"	性别:"+sex+"	民族:"+nation+"	生日:"+dr[4];
    51 
    52                     Console.WriteLine(str);
    53 
    54 
    55                 }
    56             }
    57             else
    58             {
    59                 //数据不存在
    60                 Console.WriteLine("输入的代号错误!");
    61             }
    62 
    63             conn.Close();
    64             Console.ReadLine();
    65         }
    66 
    67 
    68         static string MinZu(string code)
    69         {
    70             string name="";
    71             SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
    72             SqlCommand cmd = conn.CreateCommand();
    73             cmd.CommandText = "select Name from Nation where Code = '" + code + "'";
    74             conn.Open();
    75             SqlDataReader dr = cmd.ExecuteReader();
    76             if (dr.HasRows)
    77             {
    78                 dr.Read();
    79                 name = dr[0].ToString();
    80             }
    81             conn.Close();
    82 
    83             return name;
    84         }
    85     }
    86 }

    添加

     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 ConsoleApplication1
     9 {
    10     class Program3
    11     {
    12         static void Main3(string[] args)
    13         {
    14             //让用户输入要添加的内容
    15             Console.WriteLine("请输入要添加的代号:");
    16             string code = Console.ReadLine();
    17 
    18             Console.WriteLine("请输入姓名:");
    19             string name = Console.ReadLine();
    20 
    21             Console.WriteLine("请输入性别:");
    22             bool sex = Console.ReadLine()==""?true:false;
    23 
    24             Console.WriteLine("请输入民族:");
    25             string nation = Console.ReadLine();
    26 
    27             Console.WriteLine("请输入生日:");
    28             string birthday = Console.ReadLine();
    29 
    30             string nationcode = "n001";
    31 
    32             //将民族名称转为名族代号
    33             SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
    34             SqlCommand cmd = conn.CreateCommand();
    35             cmd.CommandText = "select Code from Nation where Name = '"+nation+"'";
    36             conn.Open();
    37             SqlDataReader dr = cmd.ExecuteReader();
    38             if (dr.HasRows)
    39             {
    40                 dr.Read();
    41                 nationcode = dr[0].ToString();
    42             }
    43             conn.Close();
    44 
    45             //往Info表添加数据
    46             cmd.CommandText = "insert into Info values('"+code+"','"+name+"','"+sex+"','"+nationcode+"','"+birthday+"')";
    47             conn.Open();
    48             cmd.ExecuteNonQuery();
    49             conn.Close();
    50             Console.WriteLine("添加成功!");
    51 
    52             Console.ReadLine();
    53         }
    54     }
    55 }
  • 相关阅读:
    邻接表怎么写
    hiho一下 第二十五周(SPFA)
    hdu 1426 Sudoku Killer(DFS)
    hdu5147 (sequence 2) 树状数组
    hdu1233 prim
    输入输出外挂
    RMQ-ST求区间最值
    最近公共祖先(简单版)
    【Java】【20】后台发送GET/POST方法
    【实战问题】【11】导入Maven项目后报错,Project configuration is not up-to-date with pom.xml. Run project configuration update
  • 原文地址:https://www.cnblogs.com/bloodPhoenix/p/5768229.html
Copyright © 2011-2022 走看看