0

In an existing codebase there is hardcoded SQL and I want to avoid SQL injection.

The below code uses SqlCommand together with SqlParameters. The query does not return any data. However, when I remove the parameters the query returns the correct results.

How can I use SqlParameters with a SELECT statement?

        string atUsername = "@username"; //does not work
        //string atUsername = "Demo1"; //THIS WORKS
        string atPassword = "@password"; //does not work
        //string atPassword = "222"; //THIS WORKS
        string sql = @"SELECT userId, userName, password, status, roleId, vendorId 
        FROM users 
        WHERE username = '" + atUsername + "' AND password = '" + atPassword + "'";

        SqlCommand cmd = new SqlCommand(sql);

        cmd.Parameters.Add(atUsername, SqlDbType.NVarChar, 20);
        cmd.Parameters[atUsername].Value = "Demo1";
        //cmd.Parameters.AddWithValue //also does not work


        cmd.Parameters.Add(atPassword, SqlDbType.NVarChar, 20);
        cmd.Parameters[atPassword].Value = "222";
        //cmd.Parameters.AddWithValue //also does not work

        SqlConnection conn = new SqlConnection(connStr);
        cmd.Connection = conn;
        conn.Open();

        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        Console.WriteLine(dt.Rows != null);
        if (dt.Rows != null)
        {
            Console.WriteLine(dt.Rows.Count);
        }

        conn.Close();
        conn.Dispose();

I have also unsuccessfully tried alternatives using

  1. SqlCommand.ExecuteReader and SqlDataReader
  2. IDisposable pattern
  3. Replace cmd.Parameters.Add(atUsername with

    SqlParameter pUsername = new SqlParameter(); pUsername.ParameterName = atUsername; pUsername.Value = "Demo1"; cmd.Parameters.Add(pUsername);"

PS. I've heard of EntityFramework but I cannot use EF in this case (long story).

4
  • 2
    Remove wrapping ' around variable names. ... = '@username' <-- wrong. ... = @username <-- right. Commented May 13, 2016 at 9:27
  • Why didn't you put it as answer? @Evk Commented May 13, 2016 at 9:39
  • @nikhilvartak Not get used yet to put such small things as answers :) Commented May 13, 2016 at 9:43
  • @Evk hehe true, it's more comfortable commenting instead in such cases. Commented May 13, 2016 at 10:31

1 Answer 1

3

The root of your problem is that you use variable names inside string literal:

 WHERE username = '@username' AND password = '@password'

So they are not treated as variable names by sql server. Instead you are searching for user with name "@username" and password "@password". Correct way is:

WHERE username = @username AND password = @password
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.