实际上万用密码就是因为SQL里面的语句--是注释,利用bug添加用户名和密码。

例如,用户名为 adada’ or 1=1--

第一个种写法登录成功了

第二种写法登录失败(正确)

第三种写法登录失败(正确)

测试代码

数据库部分

create database ThreeDb
go
USE ThreeDb;
GO
CREATE TABLE classify   --分类表
(id int primary key identity(1,1),name nvarchar(20) not null
)GO
CREATE TABLE product  --产品表
(id int primary key identity(1,1),name nvarchar(20) not null,price decimal,number int default 0,c_id int FOREIGN KEY references classify(id)
)
GOCREATE TABLE users
(id int primary key identity(1,1),name nvarchar(20) not null,pwd nvarchar(20) not null
)
GO--添加分类测试数据
insert into classify(name) values('图书');
insert into classify(name) values('家电');
insert into classify(name) values('服饰');--添加users的测试数据
insert into users(name,pwd) values('admin','admin');--添加商品测试数据
insert into product(name,price,number,c_id) values('arduino基础版',168,50,1);
insert into product(name,price,number,c_id) values('arduino提高版',268,50,1);
insert into product(name,price,number,c_id) values('arduino至尊版',468,50,1);
insert into product(name,price,number,c_id) values('比基尼',68,50,3);
insert into product(name,price,number,c_id) values('虎皮裙',168,50,3);
insert into product(name,price,number,c_id) values('长靴',368,50,3);
insert into product(name,price,number,c_id) values('电冰箱',3268,50,2);
insert into product(name,price,number,c_id) values('烘干机',2268,50,2);
GOselect count(*) from users where name='admin' and pwd='admin'
goselect count(*) from users where name='xxxdadad' or 1=1 --' and pwd='admin'
goselect * from classify;
go
select * from product order by c_id;
go

C#部分

form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;namespace AdoTestForm
{ public partial class Form1 : Form{string constr = "server=QT-201303030913;database=ThreeDb;uid=sa;pwd=daxiang";public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){comboBox1.Text = "0-->全部分类";//确定数据库连接字符串//string constr = "server=QT-201303030913;database=OneDb;uid=sa;pwd=daxiang";//string constr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;//实例化一个数据库连接的对象SqlConnection conn = new SqlConnection(constr);//打开数据库连接
            conn.Open();//实例化SqlCommand对象(该对象主要用于执行Sql命令)SqlCommand cmd = new SqlCommand();//指定要执行的SQL语句或者存储过程名称cmd.CommandText = "select id,name from classify";//确定上面为CommandText类型所赋的值是SQL语句还是存储过程名称cmd.CommandType = CommandType.Text;//指定该命令所用的数据库连接cmd.Connection = conn;//声明一个SqlDataReader(数据流对象),并将cmd执行后的结果交给它SqlDataReader sdr = cmd.ExecuteReader();//循环整个SqlDataReader对象,将里面的值取出来添加到ListBox中//sdr.Read()的作用是前进到下一条记录,这也说明SqlDataReader中的数据是一行行放置的while (sdr.Read()){//将数据流中的第一列的值添加到listBox1的项中comboBox1.Items.Add(sdr[0]+"-->"+sdr["name"]);//上面这句也可以用下面这句代替,sdr["name"]表示当前sdr的name列的值//comboBox1.Items.Add(str["name"]);
            }//关闭数据流
            sdr.Close();//关闭数据库连接
            conn.Close();using (SqlConnection conn2 = new SqlConnection(constr)){conn2.Open();SqlCommand cmd2 = new SqlCommand("select count(*) from product", conn2);string sum = cmd2.ExecuteScalar().ToString();label2.Text = string.Format("共计{0}件商品",sum);}}private void button1_Click(object sender, EventArgs e){if (comboBox1.Text != "0-->全部分类"){string constr = "server=QT-201303030913;database=ThreeDb;uid=sa;pwd=daxiang";//实例化一个数据库连接的对象SqlConnection conn = new SqlConnection(constr);//打开数据库连接
                conn.Open();//实例化SqlCommand对象(该对象主要用于执行Sql命令)SqlCommand cmd = new SqlCommand();//获取分类的id//int i = comboBox1.Text.IndexOf("-->");//获取字符串中-->所在位置索引string id = comboBox1.Text.Substring(0, 1);//只获取-->之前的字符//指定要执行的SQL语句和存储过程名字cmd.CommandText = "select * from product where c_id=" + id;//去顶上面的CommandText属性所赋值到底是sql还是存储过程名称cmd.CommandType = CommandType.Text;//指定该命令所用的数据库连接cmd.Connection = conn;//声明一个SqlDataReader(数据流对象),并将cmd执行后的结果交给它SqlDataReader sdr = cmd.ExecuteReader();//清空listBox中的项
                listBox1.Items.Clear();//循环整个SqlDataReader对象,将里面的值取出来添加到ListBox中//sdr.Read()的作用是前进到下一条记录,这也说明SqlDataReader中的数据是一行行放置的while (sdr.Read()){//将数据流中的第一列的值添加到listBox1的项中listBox1.Items.Add(sdr["id"]+":"+sdr["name"]);}//关闭数据流
                sdr.Close();//关闭数据库连接
                conn.Close();}}private void listBox1_SelectedIndexChanged(object sender, EventArgs e){button2.Text = "修改";using (SqlConnection conn = new SqlConnection(constr)){int index = listBox1.Text.IndexOf(":"); //获取-的位置string id = listBox1.Text.Substring(0,index);//只获取-之前的字符string sql = "select * from product where id=" + id;SqlCommand cmd = new SqlCommand(sql, conn);conn.Open();SqlDataReader sdr = cmd.ExecuteReader();while(sdr.Read()){tb_name.Text = sdr["name"].ToString();tb_price.Text = sdr["price"].ToString();tb_number.Text = sdr["number"].ToString();}sdr.Close();}}private void button2_Click(object sender, EventArgs e){switch (((Button)sender).Text){case "添加":using (SqlConnection conn = new SqlConnection(constr)){int index = comboBox1.Text.IndexOf("-->");string id = comboBox1.Text.Substring(0, index);if (int.Parse(id) > 0){string sql = "insert into product(name,price,number,c_id) values('"+ tb_name.Text +"',"+ tb_price.Text+ ","+ tb_number.Text +","+ id + ")";SqlCommand cmd = new SqlCommand(sql, conn);conn.Open();int i = cmd.ExecuteNonQuery();if (i > 0){MessageBox.Show("添加成功" + i.ToString() + "行数据");}else{MessageBox.Show("好囧,一行数据都没加上");}}else{MessageBox.Show("你敢不敢先选个分类,亲!");}}break;case "修改":using (SqlConnection conn = new SqlConnection(constr)){//获取当前选择行的idint index = listBox1.Text.IndexOf(":");string id = listBox1.Text.Substring(0, index);//获取分类的idint index2 = comboBox1.Text.IndexOf("-->");string c_id = comboBox1.Text.Substring(0, index2);string sql = "UPDATE product SET name = '" + tb_name.Text + "',price =" + tb_price.Text + ",number = " + tb_number.Text + ",c_id = " + c_id + "WHERE id = "+id;SqlCommand cmd = new SqlCommand(sql, conn);conn.Open();int i = cmd.ExecuteNonQuery();if (i > 0){MessageBox.Show("修改成功" + i.ToString() + "行数据");}else{MessageBox.Show("好囧,一行数据都没修改到");}}break;}}private void button3_Click(object sender, EventArgs e){using (SqlConnection conn = new SqlConnection(constr)){//获取当前选择行的idint index = listBox1.Text.IndexOf(":");string id = listBox1.Text.Substring(0, index);string sql = "delete from product WHERE id = " + id;SqlCommand cmd = new SqlCommand(sql, conn);conn.Open();int i = cmd.ExecuteNonQuery();if (i > 0){MessageBox.Show("删除成功" + i.ToString() + "行数据");}else{MessageBox.Show("好囧,一行数据都没删除到");}}}//当前窗体关闭,则推出当前程序private void Form1_FormClosing(object sender, FormClosingEventArgs e){Application.Exit();}//private void listBox1_SelectedIndexChanged(object sender, EventArgs e)//{//    if (listBox1.SelectedItems.Count > 0)//    {//        string constr = "server=QT-201303030913;database=ThreeDb;uid=sa;pwd=daxiang";//        //实例化一个数据库连接的对象//        SqlConnection conn = new SqlConnection(constr);//        //打开数据库连接//        conn.Open();//        //实例化SqlCommand对象(该对象主要用于执行Sql命令)//        SqlCommand cmd = new SqlCommand();//        //获取分类的id//        //int i = comboBox1.Text.IndexOf("-->");//获取字符串中-->所在位置索引//        string id = comboBox1.Text.Substring(0, 1);//只获取-->之前的字符//        //指定要执行的SQL语句和存储过程名字//        cmd.CommandText = "select * from product where c_id=" + id;//        //去顶上面的CommandText属性所赋值到底是sql还是存储过程名称//        cmd.CommandType = CommandType.Text;//        //指定该命令所用的数据库连接//        cmd.Connection = conn;//        //声明一个SqlDataReader(数据流对象),并将cmd执行后的结果交给它//        SqlDataReader sdr = cmd.ExecuteReader();//        //循环整个SqlDataReader对象,将里面的值取出来添加到ListBox中//        //sdr.Read()的作用是前进到下一条记录,这也说明SqlDataReader中的数据是一行行放置的//        while (sdr.Read())//        {//            if (sdr["name"].ToString() == listBox1.SelectedItem.ToString())//            {//                lbl_name.Text = sdr["name"].ToString();//                lbl_price.Text = sdr["price"].ToString();//                lbl_number.Text = sdr["number"].ToString();//                lbl_c_id.Text = comboBox1.Text;//            }//        }//        //关闭数据流//        sdr.Close();//        //关闭数据库连接//        conn.Close();//    }//}
    }
}

form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace AdoTestForm
{public partial class Form2 : Form{public Form2(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){string constr = "server=QT-201303030913;database=ThreeDb;uid=sa;pwd=daxiang";SqlConnection conn = new SqlConnection(constr);SqlCommand cmd = new SqlCommand();try{conn.Open();cmd.CommandText = "select count(*) from users where name='" + tb_uname.Text + "' and pwd = '" + tb_pwd.Text + "'";cmd.Connection = conn;int count = Convert.ToInt32(cmd.ExecuteScalar());if (count > 0){MessageBox.Show("哈哈,登陆上了哦!");}else{MessageBox.Show("插,忘记密码了!");}}catch (System.Exception ex){MessageBox.Show(ex.Message);return;}finally{conn.Close();}#region 使用参数try{conn.Open();cmd.CommandText = "select count(*) from users where name=@name and pwd=@pwd";cmd.Connection = conn;//参数cmd.Parameters.AddWithValue("@name", tb_uname.Text);cmd.Parameters.AddWithValue("@pwd", tb_pwd.Text);int count = Convert.ToInt32(cmd.ExecuteScalar());if (count > 0){MessageBox.Show("哈哈,登陆上了哦!");}else{MessageBox.Show("插,忘记密码了!");};}catch (System.Exception ex){MessageBox.Show(ex.Message);return;}finally{conn.Close();}#endregion//参数化类try{conn.Open();cmd.CommandText = "select count(*) from users where name=@name1 and pwd=@pwd1";cmd.Connection = conn;//参数//方式1SqlParameter uname = new SqlParameter("@name1", SqlDbType.VarChar, 40, "name");uname.Value = tb_uname.Text;//方式2SqlParameter pwd = new SqlParameter("@pwd1", tb_pwd.Text);cmd.Parameters.Add(uname);cmd.Parameters.Add(pwd);int count = Convert.ToInt32(cmd.ExecuteScalar());if (count > 0){MessageBox.Show("哈哈,登陆上了哦!");}else{MessageBox.Show("插,忘记密码了!");};}catch (System.Exception ex){MessageBox.Show(ex.Message);return;}finally{conn.Close();}}private void button2_Click(object sender, EventArgs e){Form1 f1 = new Form1();f1.Show();this.Hide();}}
}

form1和form2的设计界面

转载于:https://www.cnblogs.com/Mysterious/p/3420030.html

C#使用SQL语句时候的万用密码问题相关推荐

  1. mysql更改密码的sql语句_修改mysql登录密码与sql语句介绍

    修改mysql登录密码: 在使用数据库服务器过程中可能会因为种种原因忘记了登录密码,或者需要使用别人的数据库服务器,别人却忘记了登录密码等等. 忘记密码有两种方式可以解决,一是通过sql语句修改登录密 ...

  2. MS-SQL Server 基础类 - SQL语句

      网址收藏夹 免费申请! 首页 |收藏夹 | 笑话 | 贴吧 | 交友 | 留言 | 软件 | 超市 | 网页特效 | 酷站导航 | 论坛 新闻 | 同学录 | 图片 | 跑商 | 动画 | 音乐 ...

  3. 精典的SQL语句(转)

    1. 行列转换--普通 假设有张学生成绩表(CJ)如下 Name     Subject      Result 张三     语文         80 张三     数学         90 张 ...

  4. 一些sql 语句(行列转换等)

    1. 行列转换--普通 假设有张学生成绩表(CJ)如下 Name Subject Result 张三 语文 80 张三 数学 90 张三 物理 85 李四 语文 85 李四 数学 92 李四 物理 8 ...

  5. 基础类 - SQL语句

    基础类 - SQL语句 转载请标明出处: http://blog.csdn.net/hz/archive/2006/03/01/613376.asp 本文来自 CSDN 博客.x sql server ...

  6. 五百万的数据,每次查询一万条,每次到300万左右的数据查询数据就非常慢(sql语句优化对查询的巨大影响)

    前言 在做数据同步,起先还好,但是等到数据测试的时候,发现sql语句对性能影响是非常大的 测试数据库:mysql,id主键 原先 下面的语句都是简化版 select name from ceshi w ...

  7. 使用sql语句往MySQL插入1000万条数据

    在学习或者工作生产环境中,我们经常要对数据库进行压力测试,往数据库中批量插入大量数据,这里我往Mysql中批量插入大量数据,采用存储过程的方法实现. 数据库版本:Mysql5.7 一.建表 1.创建数 ...

  8. SQL语句优化技术分析

    SQL语句优化技术分析 操作符优化 IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格. 但是用IN的SQL性能总是比较低的,从ORACLE执行的步骤来分析用 ...

  9. 15000 字的 SQL 语句大全

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 链接:cnblogs.com/liuqifeng/p/914883 ...

最新文章

  1. python和R数据类型查看、赋值、列表、for循环、函数用法对比示例
  2. matlab中repmat的用法,Matlab: sum的用法、每一行求和、repmat的用法、sum和repmat结合使用减少循环...
  3. c++ 数据类型转换笔记
  4. 如何将usb连接到远程计算机,远程服务器怎么共享usb
  5. oracle存储过程 ppt,oracle_存储过程培训(动画版本)详解.ppt
  6. RabbitMQ系列教程之四:路由(Routing)
  7. android 版本28 通知栏图标,【专题分析】应用图标、通知栏适配
  8. 搭建云计算机win10,win10电脑做云服务器
  9. 1089 狼人杀-简单版 (20分)
  10. 生活大爆炸系列之磨望远镜
  11. php 音乐歌词xml,一篇文章玩转全网音乐信息库MusicBrainz API
  12. 【React源码】(十八)React 算法之调和算法
  13. Servlet的三个名字
  14. Riverbed助力富邦人寿在市场竞争和数字化进程中抢占先机
  15. USA gov data from Bitly
  16. 如何建立广泛、牢固的人脉?
  17. 怎么解决视频时摄像头显示的画面显示绿色人影的问题
  18. matlab 地质学,大类学子有话说 | 地球科学与工程学院:探寻地球的奥秘
  19. 水果库存系统(基础版)
  20. Codeforces Round #731 (Div. 3)(ABCDEFG)

热门文章

  1. java中对集合排序,Java如何对集合中的项目排序?
  2. java 文件crc校验_JavaCRC校验原理
  3. 修复Linux系统内核TCP漏洞,修复Linux TCP SACK PANIC 远程拒绝服务漏洞
  4. 抢票软件原理_手机部应届生软件大赛 | SHOW MI YOUR CODE
  5. 发票统计者 V1.0
  6. 开发日记-20190625 Linux系统管理技术手册(第二版) 第一章习题答案(个人版)
  7. (转载)Android GradientDrawable(shape标签定义) 静态使用和动态使用(圆角,渐变实现)
  8. 通过Mesos、Docker和Go,使用300行代码创建一个分布式系统
  9. sphinx 源码阅读之分词,压缩索引,倒排——单词对应的文档ID列表本质和lucene无异 也是外部排序再压缩 解压的时候需要全部扫描doc_ids列表偏移量相加获得最终的文档ID...
  10. Greenplum——升级的分布式PostgresSQL