• 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();
    
                SqlConnection conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");
                SqlCommand com = conn.CreateCommand();
                com.CommandText = "select *from Users where usename='"+name+"'";
    
                conn.Open();
                SqlDataReader dr=com.ExecuteReader();
                if (dr.HasRows)//如果查到此用户信息
                {
                    Console.Write("是否删除该用户:(Y/N)");//如果有此条信息,那么需要进行删除判断
                    string n = Console.ReadLine();//记录用户的操作
                    if (n.ToUpper() == "Y")//判断用户操作,如果是Y,说明要删除
                    {
                        conn.Close();
                        //下面的代码是真正删除此条数据
                        com.CommandText="delete from Users where usename='"+name+"'";
                        
                        int i = 0;
                        try
                        {
                            conn.Open();
                            i = com.ExecuteNonQuery();
                        }
                        catch
                        {
                            Console.WriteLine("连接服务器失败!!");
                        }
                        finally
                        {
                            conn.Close();
                        }
                        if (i > 0)//判断是否删除成功
                        {
                            Console.WriteLine("删除成功!");
                            com.CommandText = "select *from Users";//查询所有用户信息
                            conn.Open();
                            SqlDataReader dr1 = com.ExecuteReader();
                            if (dr1.HasRows)//判断是否有用户信息
                            {
                                while (dr1.Read())//判断是否有下一行,若有直接打印
                                {
                                    Console.WriteLine(dr1["code"] + "	" + dr1["usename"] + "	" + dr1["password"] + "	" + dr1["nick"] + "	" + (Convert.ToBoolean(dr1["sex"]) ? "" : "") + "	" + dr1["nation"] + "	" + dr1["class"] + "	" + (Convert.ToDateTime(dr1["birthday"]).ToShortDateString()));
                                }
                            }
                            else//无用户信息
                            {
                                Console.WriteLine("无数据!!");
                            }
                            conn.Close();
                        }
                        else
                        {
                            Console.WriteLine("删除失败!");
                        }
                    }
                    else//用户选择不删除
                    {
                        Console.WriteLine("删除操作取消!!");
                    }
                }
                else//未查到用户信息
                {
                    Console.WriteLine("用户名输入错误!!");
                }
    
    
                conn.Close();
                Console.ReadLine();
    
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    namespace update
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool has = false;//设置一中间变量判断用输入的数据是否存在,true表示存在,false不存在
    
                Console.Write("请输入要修改的用户名:");
                string name = Console.ReadLine();
    
                SqlConnection conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");
                SqlCommand com = conn.CreateCommand();
                com.CommandText = "select *from Users where usename='"+name+"'";
    
                conn.Open();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.HasRows)//如果查到此用户信息
                {
                    has = true;//将中间变量更改为true,证明有此条信息
                    if(dr.Read())//输出修改前要修改的用户的信息
                    {
                        Console.WriteLine(dr["code"]+"	"+dr["usename"]+"	"+dr["password"]+"	"+dr["nick"]+"	"+(Convert.ToBoolean(dr["sex"])?"":"")+"	"+dr["nation"]+"	"+dr["class"]+"	"+(Convert.ToDateTime(dr["birthday"]).ToShortDateString()));
                    }
                }
                conn.Close();
                if (has)
                {
                    Console.Write("已查到此用户信息,是否要修改?(Y/N)");
                    string mm = Console.ReadLine(); //记录用户的操作
                    if (mm.ToUpper() == "Y")//判断用户操作,如果是Y,说明要修改
                    {
                        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();
                        //防攻击
                        com.CommandText = "update Users set PASSWORD=@pwd,nick=@nick,sex=@sex,nation=@nation,class=@cla,birthday=@bir where usename=@name";
                        com.Parameters.Clear();
                        com.Parameters.AddWithValue("@pwd", pwd);
                        com.Parameters.AddWithValue("@nick", nick);
                        com.Parameters.AddWithValue("@sex", sex);
                        com.Parameters.AddWithValue("@nation", nation);
                        com.Parameters.AddWithValue("@cla", cla);
                        com.Parameters.AddWithValue("@name", name);
                        com.Parameters.AddWithValue("@bir", bir);
    
                        int i = 0;
                        try
                        {
                            conn.Open();
                            i = com.ExecuteNonQuery();
                        }
                        catch(Exception ex)
                        {
                            Console.WriteLine("未连接到服务器!
    "+ex);
                        }
                        finally
                        {
                            conn.Close();
                        }
                        if (i > 0)
                        {
                            Console.WriteLine("修改成功!");
                            com.CommandText = "select code,usename,password,nick,sex,(select nationname from Nation where nationcode=Users.nation) as 民族,(select classname from Class where classcode=Users.class) as 班级,birthday from Users where usename='" + name + "'";
                            conn.Open();
                            SqlDataReader dr1 = com.ExecuteReader();
                            if(dr1.HasRows)
                            {
                                while (dr1.Read())//输出修改后用户的信息
                                {
                                    Console.WriteLine(dr1["code"] + "	" + dr1["usename"] + "	" + dr1["password"] + "	" + dr1["nick"] + "	" + (Convert.ToBoolean(dr1["sex"]) ? "" : "") + "	" + dr1["民族"] + "	" + dr1["班级"] + "	" + (Convert.ToDateTime(dr1["birthday"]).ToShortDateString()));
                                }
                            }
                            conn.Close();
                        }
                        else
                        {
                            Console.WriteLine("修改失败!!");
                        }
                    }
                    else//用户选择不修改
                    {
                        Console.WriteLine("取消修改!");
                    }
                }
                else//未查到用户信息,判断has=false
                {
                    Console.WriteLine("无此用户!!");
                }
                Console.ReadLine();
    
            }
        }
    }
  • 相关阅读:
    oracle 与mysql 的当前时间比较
    easyui 时间定格为 时分
    date类型数据插入
    mac 获取idea&&datagrip激活码
    静态代码块
    nginx mac 下启动 停止 重启,查看安装位置
    定时任务的时间规则
    雅酷帮微信公众平台操作手册
    微信公众平台中通过网页增加好友
    微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断
  • 原文地址:https://www.cnblogs.com/maxin991025-/p/6113290.html
Copyright © 2020-2023  润新知