Click here to Skip to main content
15,921,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

im pretty new using ado.net for connecting to a database, but im totally new to getting my query's sql injection proof, i'am using it like this,
C#
OleDbConnection dbcon = new OleDbConnection();
OleDbDataReader dr;
dbcon.connectionstring = ....connection string setup;
OleDbCommand dbc = new OleDbCommand("SELECT * FROM test WHERE id=@id", dbcon);
cmd.Parameters.AddWithValue("@id",id);
dr = dbc.ExecuteReader();
while (dr.Read())
{
  ...code here
}

but i dont know exactually in wich situations you need to do this.

1. all the query's such as: insert,select,update,delete.
2. only query's wich requires user input from textboxes for example.

And before i used the parameters i could see the litteral input when i was debugging my code by watching the dbc.Commandtext, but now its just filled with the placeholders like @id etc.

Is there any way i can see the litteral query with the real value's?

Thanks in advance.
Posted
Updated 5-Apr-13 7:44am
v2

Basically, you need to use parametrized statements: http://en.wikipedia.org/wiki/SQL_injection#Parameterized_statements[^].

Please see my past answers for some more detail:
hi name is not displaying in name?[^],
EROR IN UPATE in com.ExecuteNonQuery();[^].

Here is a good illustration of SQL injection: http://xkcd.com/327/[^] :-).

—SA
 
Share this answer
 
You can use SQL Profiler (a tool you run to see all queries executed against a database) to see the literal query and values. Though, you will find that the query may even be sent to the server parameterized, followed by some statements that pass in the values of the parameters.

Yes, all queries that take parameters should use parameterized queries. Never do string concatenation. There is usually no good reason to ever do manual string concatenation.

If you decided to use an ORM (NHibernate, Entity Framework), you may not even see the query... they will be built and parameterized for you. This is another good reason to use SQL Profiler.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900