• ado.net 修改,查询


    修改:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    namespace 修改
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("请输入要修改的成员姓名:");
                string name = Console.ReadLine();
                Console.Write("请输入要修改的成员的生日:");
                string bir = Console.ReadLine();
    
                //创建数据库连接类
                SqlConnection conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123;");//编写连接字符串
    
                //创建数据库操作类,创建过程是与刚创建的连接对象匹配起来
                SqlCommand comm = conn.CreateCommand();
                //编写操作语句 TSQL语句
                comm.CommandText="update Users set birthday='"+bir+"' where usename='"+name+"'";
    
                conn.Open();
                int i = comm.ExecuteNonQuery();
                conn.Close();
                if (i > 0)
                    Console.WriteLine("修改成功!");
                else
                    Console.WriteLine("修改失败!");
                Console.ReadKey();
    
    
    
    
            }
        }
    }

    查询:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建数据库连接类
                SqlConnection con = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123;");
    
                //创建数据库操作类,创建过程是与刚创建的连接对象匹配起来
                SqlCommand com = con.CreateCommand();
                //编写操作语句 TSQL语句
                com.CommandText = "select code,usename,password,nick,sex,nation,class,(select nationname from Nation where nationcode=Users.nation) as 民族,(select classname from Class where classcode=Users.class) as 班级,birthday from Users";//
                con.Open();
                //读取操作,返回读取器对象
                SqlDataReader dr= com.ExecuteReader();
                if (dr.HasRows)//获取一个bool值,判断是否含有一行或多行
                {
                    while (dr.Read())//获取一个bool值,前进到下一条记录
                    {
                        Console.WriteLine(dr["code"] + "	" + dr["usename"] + "	" + dr["password"] + "	" + dr["nick"] + "	" + (Convert.ToBoolean(dr["sex"]) ? "" : "") + "	" + dr["民族"] + "	" + dr["班级"] + "	" + (Convert.ToDateTime(dr["birthday"]).ToShortDateString()));
                    }
                }
    
                con.Close();
                Console.ReadLine();
    
            }
        }
    }
  • 相关阅读:
    python--触发器, 储存过程, 事务
    python--MySQL权限管理 数据备份还原
    python--MySQL多表查询
    python--MySQl单表查询
    python--MySQL数据库初识
    WPF学习(7)命令
    WPF学习(6)路由事件
    WPF学习(5)依赖属性
    WPF学习(4)逻辑树和可视树
    WPF学习(3)布局
  • 原文地址:https://www.cnblogs.com/maxin991025-/p/6109794.html
Copyright © 2020-2023  润新知