Search code examples
c#.netsql-injection

How to prevent a SQL Injection escaping strings


I have some queries (to an acccess database) like this :

string comando = "SELECT * FROM ANAGRAFICA WHERE E_MAIL='" + user + "' AND PASSWORD_AZIENDA='" + password + "'";

and I'd like to "escape" user and password, preventing an injection.

How can I do it with C# and .NET 3.5? I'm searching somethings like mysql_escape_string on PHP...


Solution

  • You need to use parameters. Well dont have to but would be preferable.

    SqlParameter[] myparm = new SqlParameter[2];
    myparm[0] = new SqlParameter("@User",user);
    myparm[1] = new SqlParameter("@Pass",password);
    
    string comando = "SELECT * FROM ANAGRAFICA WHERE E_MAIL=@User AND PASSWORD_AZIENDA=@Pass";