• ado.net


    使用前先添加

    using System.Data.SqlClient;

    delete删除:

    namespace adonet_接受_删除_添加
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入要删除的用户名:");
                string uname = Console.ReadLine();
                //1、创建数据库链接类
                string sql = "server=.;database=data0928;user=sa;pwd=123;";
                SqlConnection conn = new SqlConnection(sql);
                //2、创建数据库操作类
                SqlCommand cmd = conn.CreateCommand();
                //3、编写操作语句
                cmd.CommandText = "delete from users where username='" + uname + "'";
                //4、打开数据库连接
                conn.Open();
                //5、执行操作,返回受影响行数
                int a= cmd.ExecuteNonQuery();
                //6、关闭数据库连接
                conn.Close();
                //7、提示操作是否成功
                if (a > 0)
                { Console.WriteLine("删除成功"); }
                else
                { Console.WriteLine("删除失败"); }
    
                Console.ReadLine();
            }
        }
    }

    insert添加:

    namespace insert
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入要添加的用户名:");
                string uname = Console.ReadLine();
                Console.WriteLine("请输入要添加的密码:");
                string pwd = Console.ReadLine();
                Console.WriteLine("请输入要添加的昵称:");
                string nick = Console.ReadLine();
                Console.WriteLine("请输入要添加的性别:");
                string sex = Console.ReadLine();
                Console.WriteLine("请输入要添加的生日:");
                string bir = Console.ReadLine();
                Console.WriteLine("请输入要添加的民族:");
                string nation = Console.ReadLine();
                Console.WriteLine("请输入要添加的班级:");
                string cla = Console.ReadLine();
    
                //创建数据库连接类
                SqlConnection con = new SqlConnection("server=.;database=data0928;user=sa;pwd=123");
                //创建数据库操作类
                SqlCommand cm = con.CreateCommand();
                //编写操作语句
                cm.CommandText = "insert into users values('" + uname + "','" + pwd + "','" + nick + "','" + sex + "','" + bir + "','" + nation + "','" + cla + "')";
                //打开数据库连接
                con.Open();
                //执行操作
                cm.ExecuteNonQuery();
                //关闭数据库
                con.Close();
    
                Console.ReadLine();
            }
        }
    }

    select选择:

    namespace select
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建连接
                SqlConnection conn = new SqlConnection("server=.;database=data0928;user=sa;pwd=123");
                //创建数据库操作
                SqlCommand cmd = conn.CreateCommand();
                //编写操作语句
                cmd.CommandText = "select * from users";
    
                conn.Open();
                SqlDataReader dr= cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Console.WriteLine(dr["ids"] + "	" + dr["username"] + "	" + dr["password"] + "	" + dr["nickname"] + "	" + dr["sex"] + "	" + dr["birthday"] + "	" + dr["nation"] + "	" + dr["class"]);
                    }            
                }
                conn.Close();
                Console.ReadLine();
            }
        }
    }

    update更改:

    namespace Update
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("请输入要修改的用户名:");
                string uname = Console.ReadLine();
                Console.Write("请输入密码:");
                string pwd = Console.ReadLine();
                Console.Write("请输入昵称:");
                string nick = Console.ReadLine();
                Console.Write("请输入性别:");
                string sex = Console.ReadLine();
                Console.Write("请输入生日:");
                string bir = Console.ReadLine();
                Console.Write("请输入民族:");
                string nation = Console.ReadLine();
                Console.Write("请输入班级:");
                string cla = Console.ReadLine();
    
                SqlConnection conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=123");
    
                SqlCommand cmd = conn.CreateCommand();
    
                cmd.CommandText = "update Users set PassWord = '" + pwd + "',NickName = '" + nick + "',Sex='" + sex + "',Birthday='" + bir + "',Nation='" + nation + "',Class='" + cla + "' where UserName = '" + uname + "'";
    
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
    
            }
        }
    }
  • 相关阅读:
    每日日报
    每日日报
    每日日报
    线上问题定位---jstack
    8.18Java入门--->第二十二节(ObjectOutputStream、ObjectInputStream)
    8.17Java入门--->第二十一节(IO流)
    Mybatis--->第六节一对多和多对一
    8.14Java入门--->第二十节
    8.13Java入门--->第十九节(Map嵌套)
    Java学习
  • 原文地址:https://www.cnblogs.com/wy1992/p/6110979.html
Copyright © 2020-2023  润新知