Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / WCF

ADO.NET – How to Prevent SQL Injection Attack?

4.50/5 (2 votes)
24 Feb 2014CPOL1 min read 18.8K  
This post goes over the prevention of SQL injection attack in ADO.NET

Introduction

In the last articles on ADO.NET, we have discussed about SQL injection attack. You can read those articles here. In this article, we will go over prevention of SQL injection attack in ADO.NET.

We can avoid SQL injection attacks in two ways:

  1. Using parameterized queries
  2. Using stored procedures

Let’s examine one by one. The tblProductInventory table which we have used in the previous article is as follows:

tblProductInventory

As we know, the following lines of code were causing SQL injection attack.

C#
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
string Command="Select * from tblProductInventory where ProductName like ‘"+TextBox1.Text     + "%’";
    SqlCommand cmd = new SqlCommand(Command, con);
    con.Open();
    GridView1.DataSource = cmd.ExecuteReader();
    GridView1.DataBind();
}

Using Parameterized Queries

The above code can easily be rewritten using parameterized queries to prevent SQL injection attack. 

C#
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
// Parameterized query. @ProductName is the parameter
  string Command = "Select * from tblProductInventory _<br />  where ProductName like @ProductName";SqlCommand cmd = new SqlCommand(Command, con);
// Provide the value for the parameter
    cmd.Parameters.AddWithValue("@ProductName", TextBox1.Text + "%");
    con.Open();
    GridView1.DataSource = cmd.ExecuteReader();
    GridView1.DataBind();
}

Notice that the query now uses parameter @ProductName. The value for this parameter is then provided using AddWithValue() method. The parameter is associated with the command object using Parameters collection property of the command object.

Now if we type the following input into the TextBox, the entire content of the TextBox is treated as a value for the parameter @ProductName, not as a separate SQL statement. Thereby, it removes SQL injection attack.

SQL
i’; Delete from tblProductInventory –

Using Stored Procedures

SQL injection can also be prevented using stored procedures. So first, let’s write a stored procedure that returns the list of products. This stored procedure takes an input parameter @ProductName.

C#
CREATE PROCEDURE spGetProductsByName
@ProductName nvarchar(50)
AS
BEGIN
SELECT * FROM tblProductInventory 
WHERE ProductName LIKE @ProductName + ‘%’
END

To test this procedure, execute the following command in SQL Server Management Studio (SSMS).

SQL
EXECUTE spGetProductsByName ‘ip’

As a result of the above query, all the products whose name starts with ‘ip’ will be returned.

Now, let’s rewrite the code to use stored procedure spGetProductsByName.

C#
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
// The command that we want to execute is a stored procedure,
// so specify the name of the procedure as cmdText
    SqlCommand cmd = new SqlCommand("spGetProductsByName", con);
// Specify that the T-SQL command is a stored procedure
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
// Associate the parameter and it’s value with the command object
    cmd.Parameters.AddWithValue("@ProductName", TextBox1.Text + "%");
    con.Open();
    GridView1.DataSource = cmd.ExecuteReader();
    GridView1.DataBind();
}

The above code as well will treat the entire content of the TextBox as a value for @ProductName and hence avoid SQL injection attack.

So the main point here to ponder is, always use parameterized queries or stored procedures to avoid SQL injection attacks.

Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)