C#使用SQL语句时候的万用密码问题
实际上万用密码就是因为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)
)
GO CREATE 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);
GO select count(*) from users where name='admin' and pwd='admin'
go select count(*) from users where name='xxxdadad' or 1=1 --' and pwd='admin'
go select * 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[]+"-->"+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(, );//只获取-->之前的字符 //指定要执行的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(,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(, index); if (int.Parse(id) > )
{
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 > )
{
MessageBox.Show("添加成功" + i.ToString() + "行数据");
}
else
{
MessageBox.Show("好囧,一行数据都没加上");
} }
else
{
MessageBox.Show("你敢不敢先选个分类,亲!");
}
}
break;
case "修改":
using (SqlConnection conn = new SqlConnection(constr))
{
//获取当前选择行的id
int index = listBox1.Text.IndexOf(":");
string id = listBox1.Text.Substring(, index); //获取分类的id
int index2 = comboBox1.Text.IndexOf("-->");
string c_id = comboBox1.Text.Substring(, 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 > )
{
MessageBox.Show("修改成功" + i.ToString() + "行数据");
}
else
{
MessageBox.Show("好囧,一行数据都没修改到");
} } break;
}
} private void button3_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(constr))
{
//获取当前选择行的id
int index = listBox1.Text.IndexOf(":");
string id = listBox1.Text.Substring(, index); string sql = "delete from product WHERE id = " + id;
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open(); int i = cmd.ExecuteNonQuery();
if (i > )
{
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 > )
{
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 > )
{
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; //参数
//方式1
SqlParameter uname = new SqlParameter("@name1", SqlDbType.VarChar, , "name");
uname.Value = tb_uname.Text;
//方式2
SqlParameter pwd = new SqlParameter("@pwd1", tb_pwd.Text); cmd.Parameters.Add(uname);
cmd.Parameters.Add(pwd); int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > )
{
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的设计界面

C#使用SQL语句时候的万用密码问题的更多相关文章
- 精妙的SQL语句
说明:复制表(只复制结构,源表名:a 新表名:b)select * into b from a where 1<>1 说明:拷贝表(拷贝数据,源表名:a 目标表名:b)insert i ...
- ORACLE导入、导出所有数据到文件的SQL语句
打开cmd窗口,执行如下SQL语句即可 --导出 exp 用户名/密码@localhost/orcl file=d:\111.dump log=d:111.log --或者 1.登录管理员system ...
- SQL语句大全
经典SQL语句大全(绝对的经典) 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份s ...
- SQL语句优化(转载)
一.操作符优化 1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用 ...
- sql语句优化SQL Server
MS SQL Server查询优化方法查询速度慢的原因很多,常见如下几种 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了 ...
- 实用SQL语句大全
一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...
- 改善SQL语句(转)
二.改善SQL语句 很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select * from ta ...
- 经典SQL语句大全
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- SQL 语句大全(转载)
经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...
随机推荐
- NSDate 类的总结,全面基础
<span style="font-size:24px;"><span style="font-size:18px;">//1.创建日期 ...
- <context:annotation-config/>、<context:component-scan/>
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册 AutowiredA ...
- Android SDK Manager 下载SDK失败的解决办法
摘要:本文记录了无法使用Android SDK Manager下载SDK开发包的解决办法. 最近需要进行android应用程序的开发工作,在android官网下载了adt-bundle-linux- ...
- Git合并分支命令参数详解:git merge --ff
今天研究了一下git merge命令常用参数,并分别用简单的例子实验了一下,整理如下: 输入命令git merge -h可以查看相关参数: --ff 快速合并,这个是默认的参数.如果合并过程出现冲突 ...
- IIS管理器如何添加网站
IIS服务器一些步骤 安装好iis后 右击网站按钮点击添加网站 网站名称填写无所谓,物理路径(注意是大路径,一个项目所有的文件在那个文件夹下), Ip地址自己定义最好是hosts文件已经绑定了域名的, ...
- oracle查看用户有哪些权限和角色
select * from dba_sys_privs t where t.grantee='HR';select * from dba_role_privs t where t.grantee='H ...
- swift - 全屏pop手势
UINavigationController系统自带有侧滑手势,但是这个手势第一点只能边缘侧滑才可以有效,第二点当手动隐藏系统的导航时,这个手势就不能生效了 为了能到达到全屏pop的效果这里有2中解决 ...
- IIS中使用ASP.NET MVC的经验总结
在这篇文章中我们学习在不同版本的IIS中使用ASP.NET MVC和URL Routing.我们学习针对IIS7.0.IIS6.0和更早版本的IIS的处理策略. ASP.NET MVC框架依赖于URL ...
- 创建自己的Spring Boot Starter
抽取通用模块作为项目的一个spring boot starter.可参照mybatis的写法. IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入sp ...
- Elasticsearch5.X IN Windows 10 系列文章(1)
系统类型:windows10 64位家庭版 ElasticSearch版本: 5.5.1 (最新稳定版为5.5.2),由于用到IK中文分词插件,最新版本没有5.5.2 ,所以使用5.5.1 日期:2 ...