ADO.NET防止字符串攻击方法

在黑窗口里面输入内容时利用拼接语句可以对数据进行攻击

如:输入班级值

--代表后边内容都被注释掉了

防止SQL注入攻击方法

再给命令发送SQL语句的时候分两次发送,把SQL语句拆成两块,用户输入的是一块;本身写好的是一块,第一次把CommandText里写的sql语句发过去;第二次把变量值发过去,进行匹配

cmd.CommandText = "update Users set PassWord=@pwd"
cmd.Parameters.AddWithValue("@pwd",pwd);

栗子:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Update
{
    class Program
    {
        static void Main(string[] args)
        {
            bool has = false;//判断用输入的数据是否存在,true表示存在,false不存在

            Console.Write("请输入要修改的用户名:");
            string uname = Console.ReadLine();

            SqlConnection conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=123");
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select *from Users where UserName = '" + uname + "'";

            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows) //如果查到此用户信息
            {
                has = true;//将中间变量更改为true,证明有此条信息
            }
            conn.Close();

            if (has)//如果有此条信息,那么需要进行删除判断
            {
                Console.Write("已查到此用户信息,是否要修改?(Y/N)");
                string u = Console.ReadLine(); //记录用户的操作
                if (u.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();

                    //使**=一个变量
                    cmd.CommandText = "update Users set PassWord=@pwd,NickName=@nick,Sex=@sex,Birthday=@bir,Nation=@nation,Class=@cla where UserName=@uname";
                    cmd.Parameters.Clear();//清除绑定的变量,最好每次用参数集合前写一个清除
                    //格式:cmd.Parameters.AddWithValue("@**",**);有多少列绑多少个
                    cmd.Parameters.AddWithValue("@pwd", pwd);
                    cmd.Parameters.AddWithValue("@nick", nick);
                    cmd.Parameters.AddWithValue("@sex", sex);
                    cmd.Parameters.AddWithValue("@bir", bir);
                    cmd.Parameters.AddWithValue("@nation", nation);
                    cmd.Parameters.AddWithValue("@cla", cla);
                    cmd.Parameters.AddWithValue("@uname", uname);

                    conn.Open();
                    //执行操作,并记录受影响的行数
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    Console.WriteLine("修改成功!");
                }
                else//用户选择不删除
                {
                    Console.WriteLine("修改取消!");
                }
            }
            else//未查到用户信息,判断has=false
            {
                Console.WriteLine("用户名输入错误!未查到此用户信息!");
            }


            Console.ReadKey();



        }
    }
}
原文地址:https://www.cnblogs.com/jiuban2391/p/6112907.html