当前位置:网站首页>Executing SQL statements with parameterized commands
Executing SQL statements with parameterized commands
2022-06-24 18:39:00 【MousseIn】
Parameterized command execution sql sentence
Understand the benefits of parameterized instructions
- prevent sql Inject
string sb = “SELECT *FROM admin WHERE loginid =’”+loginId+"‘AND loginPwd =’"+logingPwd+"’"
string sb =“SELECT * FROM admin WHERE loginid =” or 1=1 --AND loginPwd = ‘xxx’
Use this splicing method , Two disadvantages :
- Not beautiful , And it's easy to make mistakes .
- Poor safety , Easy to be sql Injection attack .
Examples are as follows :
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Please enter a user name :");
string name = Console.ReadLine();
Console.WriteLine(" Please input a password :");
string pwd = Console.ReadLine();
int count = 0;
string conStr = "data source=(local);database=MySchool;uid=sa;pwd=123456";
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
string sql = "select count(*) from dbo.student where studentno = "+ name +" and loginpwd = '"+pwd +"'";
SqlCommand cmd = new SqlCommand(sql,con);
count = Convert.ToInt32(cmd.ExecuteScalar());
}
if (count > 0)
{
Console.WriteLine(" Landing successful !");
}
else
{
Console.WriteLine(" Login failed !");
}
Console.ReadKey();
}
}
}
I use count This parameter is used to receive the number of affected rows returned , If the number of returned rows is greater than zero , Login succeeded , If the number of returned rows is less than zero , Login failed .
If we enter... In the user name column "3 or 1=1 --" The original statement to go to the database
select count(*) from dbo.student where studentno = '23214' and loginpwd = '0000'
Turned into
select count(*) from dbo.student where studentno = 3 or 1 = 1 -- and loginpwd = '123456'
Heng holds and the database password is commented out .
Execution results count The return value is fixed to have a value , Therefore, the login is successful .
Parameterized query is an implementation that can effectively avoid the above drawbacks sql How statements work .
Master the use of parameterized instructions to execute sql sentence
Use @ Construct parameterization sql command
sqlParameter object
- and “@” The decorated parameters correspond to each other , To replace this parameter
adopt Commend Of Parameters Attribute addition SqlParameter object
SqlParameter Properties of
- DbType The data type of the corresponding database
- Direction Parameters can only be entered 、 No output 、 Bidirectional or stored procedure return value parameter
- IsNullable Whether the parameter accepts null values
- ParameterName Name of parameter
- Size The maximum size of the data corresponding to the parameter
- SqlDbType SQL Server The data type of the database
- Value The value of the parameter
Use SqlCommmand Perform parameterization SQL Steps for
- Construct database link objects
- Construct parameterization SQL sentence
- structure SqlParameter object
- establish SqlCommand object , Use SqlParameter Object fill data
- Open database link , perform SqlCommand command
Examples are as follows :
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CH03
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Please enter a user name :");
string name = Console.ReadLine();
Console.WriteLine(" Please input a password :");
string pwd = Console.ReadLine();
int count = 0;
string conStr = "data source=(local);database=MySchool;uid=sa;pwd=123456";
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
// Build parameterization sql sentence
string sql = "select count(*) from student where studentno [email protected] and loginpwd = @pwd";
// Build the value to replace , structure sqlparameter object
SqlParameter[] par = {
new SqlParameter("@n",name),// relation
new SqlParameter("@pwd",pwd)
};
//string sql = "select count(*) from dbo.student where studentno = "+ name +" and loginpwd = '"+pwd +"'";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.AddRange(par);// Add a parameterized array to cmd in
count = Convert.ToInt32(cmd.ExecuteScalar());
}
if (count > 0)
{
Console.WriteLine(" Landing successful !");
}
else
{
Console.WriteLine(" Login failed !");
}
Console.ReadKey();
}
}
}
Input again sql Injection method :
Incoming sql Statement for :
select count(*) from student where studentno [email protected] and loginpwd = @pwd
Rather than using + No sql sentence .
The advantage is that :
- High safety , Effectively avoid SQL Inject
- The code is simple and beautiful
边栏推荐
- 717.1-bit and 2-bit characters [sliding window]
- Palindrome string (two methods)
- Gateway solves cross domain access
- Microservice system design - sub service project construction
- JS deep understanding of functions
- Different JVM
- Leetcode question 136 [single number]
- Specification for self test requirements of program developers
- Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot
- Business leaders compete for CIO roles
猜你喜欢

Considerations for it project demand analysis

Conception de systèmes de micro - services - construction de sous - services

Wechat applet development - Implementation of rotation chart

Recommend 14 commonly used test development tools

Get max value of a bit column - get max value of a bit column

Mcu-08 interrupt system and external interrupt application

How to use Fisher's least significant difference (LSD) in R

Window object

Three layer switching experiment

Microservice system design -- interface document management design
随机推荐
Wechat applet development - Implementation of rotation chart
Redpacketframe and openmode packages
About whether arm's large and small end mode is related to CPU or compiler
How about China Power Investment Xianrong futures? Is it safe to open futures accounts?
SAP license: ERP for supply chain management and Implementation
Application service access configuration parameters
How to select the best test cases for automation?
What is business intelligence (BI)?
Redis series (3) - sentry highly available
Mcu-08 interrupt system and external interrupt application
next_ Permutation full permutation function
Window object
JS deep understanding of scope
Leetcode daily question solution: 717 1-bit and 2-bit characters - reverse order
Restcloud ETL extracting dynamic library table data
SAP license:sap s/4hana is the answer
如何在 R 中执行稳健回归
Uniapp wechat applet calls mobile map to navigate to the target point
Leetcode question 136 [single number]
What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?