• ADO.NET增删改


    static void Main1(string[] args)
    {
    添加
    造连接字符串
    string connstring = "server=.;database=mydb;user=sa;pwd=100867";
    造连接对象
     SqlConnection conn = new SqlConnection(connstring);
    造命令对象 直接生成一个命令对象
    SqlCommand cmd= conn.CreateCommand();
    给一个SQL语句
    cmd.CommandText = "insert into Info values('p087','李丽','true','n001','1988-2-10')";
    cmd.CommandText = "delete from Info where code='p087'";
     cmd.CommandText = "update Info set name='张三' where code='p002'";
    打开连接
     conn.Open();
       cmd.ExecuteNonQuery();
    if (n > 0)
    {
     Console.WriteLine("添加成功!");
    }
    else
    {
     Console.WriteLine("添加失败!");
    }

     conn.Close();

     Console.ReadLine();

    }

    让用户输入要添加的内容
    Console.WriteLine("请输入要添加的代号:");
    string code = Console.ReadLine();
    Console.WriteLine("请输入姓名:");
    string name = Console.ReadLine();
    Console.WriteLine("请输入性别:");
    bool sex= Console.ReadLine() == "男" ? true : false;
    Console.WriteLine("请输入民族:");
    string nation = Console.ReadLine();
    Console.WriteLine("请输入生日:");
    string birthday = Console.ReadLine();
    string nationcode = "noo1";
    //将民族名称转为民族代号
    SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=100867");
    SqlCommand cmd= conn.CreateCommand();
    cmd.CommandText = "select code from nation where name='"+nation+"'";
    conn.Open();
    SqlDataReader dr= cmd.ExecuteReader();
    if (dr.HasRows)
    {
    dr.Read();
    nationcode= dr[0].ToString();
    }
    conn.Close();
    //往INFO表中添加数据 民族表也可以添加了
    cmd.CommandText = "insert into Info values('"+code+"','"+name+"','"+sex+"','"+nationcode+"','"+birthday+"')";
    conn.Open();
    cmd.ExecuteNonQuery();

    Console.WriteLine("添加成功!");
    conn.Close();

    Console.ReadLine();

    用户输入要删除的数据主键值
    Console.WriteLine("请输入要删除的代号:");
    string code = Console.ReadLine();
    //先判断该数据库存不存在——根据主键值去查数据库
    SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=100867");
    SqlCommand cmd= conn.CreateCommand();
    cmd.CommandText = "select * from Info where code='"+code+"'";
    conn.Open();
    SqlDataReader dr= cmd.ExecuteReader();;

    if (dr.HasRows)
    {
    //说明该数据存在
    Console.WriteLine("查到该数据,是否要执行删除操作,如果要删除请输入:1");
    int sc=Convert.ToInt32( Console.ReadLine());
    if (sc == 1)
    {
    //删除
    dr.Close();//关闭读取器
    cmd.CommandText = "delete from Info where code='"+code+"'";

    cmd.ExecuteNonQuery();
    Console.WriteLine("删除成功!");

    }
    else
    {
    //不删除
    dr.Read();
    string sex= Convert.ToBoolean(dr[2])?"男":"女";
    string nation = minzu(dr[3].ToString());
    string str = "代号:"+dr[0]+"姓名:"+dr[1]+"性别:"+sex+"民族:"+nation+"生日:"+dr[4];

    Console.WriteLine(str);

    }
    }
    else
    {
    //数据不存在
    Console.WriteLine("输入的代号错误!");
    }

    conn.Close();
    Console.ReadLine();

    }
    static string minzu(string code)
    {
    string name = "";
    SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=100867");
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "select name from nation where code='" + code + "'";
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
    dr.Read();
    name = dr[0].ToString();
    }
    conn.Close();
    return name;
    }

  • 相关阅读:
    Mybatis多层嵌套查询
    UUID 唯一性实现原理
    oracle 多实例启动
    orcal启动多实例是报 ORA-00845: MEMORY_TARGET not supported onthis system
    java调用quartz 2.2.2方法总结。
    mybatis中like的使用(模糊查询)
    Orcal数据库实现主键ID自增
    spring cloud分布式关于熔断器
    spring cloud分布式健康检查
    spring cloud分布式整合zipkin的链路跟踪
  • 原文地址:https://www.cnblogs.com/yp11/p/5775021.html
Copyright © 2020-2023  润新知