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 ...
随机推荐
- nginx的buffered to a temporary警告
nginx日志报a client request body is buffered to a temporary file 这个意思是客户全请求的文件超过了nginx的缓存区大小,nginx将内容写入 ...
- iOS项目开发实战——使用同步请求获取网页源码
网络请求一般分为同步请求和异步请求,同步请求假设訪问时间过长,会造成界面卡死状态,用户体验不是非常好.可是请求速度较快的话,也能够考虑使用同步訪问.如今先来学习同步訪问. (1)在viewDidLoa ...
- apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件或目录执行权限的设置方法
apache环境下禁止某文件夹内运行PHP脚本.禁止访问文件或目录执行权限的设置方法 首先我们来看两段对上传目录设置无权限的列子,配置如下: <Directory "要去掉PHP执 ...
- [LeetCode] Restore IP Address [28]
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- Oracle 修改表名
.ALTER TABLE T_PLAT_KEYWORD_STATISTIC RENAME TO T_PLAT_KEYWORD; .create new_table as select * from o ...
- Hadoop eclipse插件使用过程中出现的问题
http://download.csdn.net/detail/java2000_wl/4326323 转自http://www.ithao123.cn/content-945210.html 由于h ...
- python random 学习
随机产生8位数,每位数都是1~6之间数字 import random list_1 = [i for i in xrange(1,7)] print list_1 arr_1=[] for i in ...
- VLOOKUP函数的用法
https://zhidao.baidu.com/question/177265501.html?qbl=relate_question_0&word=%3DVLOOKUP%28B10%2CS ...
- 在集群中使用文件加载graph
从hdfs上加载文件并创建graph scala> var graphs = GraphLoader.edgeListFile(sc,"/tmp/dataTest/graphTest. ...
- spring boot配置文件
1.spring boot通常打成一个jar文件发布,想修改配置文件比较麻烦,但他提供了一种读取外部配置文件的方式.在代码的主类中增加如下代码 System.setProperty("spr ...