C#中用基本的方法对数据库进行增删改查,会被黑客利用,写入其他的代码以实现对数据库的数据进行其他的操作。例如:

对下列数据库的某个信息进行修改操作

C#-黑客-数据库访问-字符串的攻击和防御

修改代码:

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

namespace 攻击_防御
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建 数据库连接类
            SqlConnection conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=asdf;");
            //创建 数据库操作类
            SqlCommand cmd = conn.CreateCommand();

            //一、显示Users表中的所有信息
            cmd.CommandText = "select *from Users";

            //在数据库中执行操作
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
                Console.WriteLine(dr["ids"] + "\t" + dr["Username"] + "\t" + dr["password"] + "\t" + dr["nickname"] + "\t" + dr["sex"] + "\t" + dr["birthday"] + "\t" + dr["nation"] + "\t" + dr["class"] + "\t");
            conn.Close();
            //二、让用户选择要修改的数据
            Console.WriteLine();
            Console.Write("请输入要修改数据的用户名:");
            string uname = Console.ReadLine();

            //在数据库查询有无此信息
            cmd.CommandText = "select *from Users where username='" + uname + "'";
            bool has = false;
            conn.Open();
            SqlDataReader dr1 = cmd.ExecuteReader();
            if (dr1.HasRows)
                has = true;
            conn.Close();

            //提示有无此信息,是否进行修改
            if (has)
            {
                Console.WriteLine("已查到此用户信息,请输入修改后的信息");
                Console.Write("请输入修改的用户名:");
                string xname = Console.ReadLine();
                Console.Write("请输入修改的密码:");
                string xpwd = Console.ReadLine();
                Console.Write("请输入修改的昵称:");
                string xnick = Console.ReadLine();
                Console.Write("请输入修改的性别:");
                bool xsex = Convert.ToBoolean(Console.ReadLine());
                Console.Write("请输入修改的生日:");
                DateTime xbir = Convert.ToDateTime(Console.ReadLine());
                Console.Write("请输入修改的民族:");
                string xnation = Console.ReadLine();
                Console.Write("请输入修改的班级:");
                string xcla = Console.ReadLine();

                //修改信息准备操作
                cmd.CommandText = "update Users set username='" + xname + "',password='" + xpwd + "',nickname='" + xnick + "',sex='" + xsex + "',birthday='" + xbir + "',nation='" + xnation + "',class='" + xcla + "' where username='" + uname + "'";
                //在数据库中执行操作
                conn.Open();
                int i0 = cmd.ExecuteNonQuery();

                conn.Close();
                //判断是否修改成功
                if (i0 > 0)
                    Console.WriteLine("数据修改成功!");
                else
                    Console.WriteLine("数据修改失败!");


            }
            else
                Console.WriteLine("查无此信息。");

            Console.ReadLine();
        }
    }
}
对数据库数据进行修改操作

相关文章:

  • 2021-12-19
  • 2022-12-23
  • 2021-09-20
  • 2022-01-02
  • 2021-11-23
  • 2022-01-18
  • 2021-12-24
猜你喜欢
  • 2022-12-23
  • 2021-09-13
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2021-12-30
相关资源
相似解决方案