Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I think I'm making a fairly amateur mistake somewhere here, but I can't get SQL Parameters to reliably work in C#. Consider the following code:

        protected string[] Query(string dataToFind, string tableName, string fieldToCheck, string fieldToReturn)
    {
        SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

        SqlDataReader dataReader = null;
        SqlCommand command = connection.CreateCommand();

        command.CommandText = "SELECT " + fieldToReturn + " FROM " + tableName + " WHERE " + fieldToCheck " = '" + dataToFind "'";

        try
        {
            connection.Open();
            dataReader = command.ExecuteReader();
etc...

This executes as you would expect, returning the fieldToReturn from the table tableName. However, I understand that this is vulnerably to SQL injections, and that the correct way to avoid this is to use parameters. So I change my code to the following:

protected string[] Query(string dataToFind, string tableName, string fieldToCheck, string fieldToReturn)
    {
        SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

        SqlParameter[] parameters = new SqlParameter[4];
        parameters[0] = new SqlParameter("@dataToFind", dataToFind);
        parameters[1] = new SqlParameter("@name", tableName);
        parameters[2] = new SqlParameter("@fieldToCheck", fieldToCheck);
        parameters[3] = new SqlParameter("@fieldToReturn", fieldToReturn);

        SqlDataReader dataReader = null;
        SqlCommand command = connection.CreateCommand();
        command.Parameters.AddRange(parameters);

        command.CommandText = "SELECT @fieldToReturn FROM @tableName WHERE @fieldToCheck = @dataToReturn";

        try
        {
            connection.Open();
            dataReader = command.ExecuteReader();
etc...

If I have 3 matches in my database, the first code example returns 3 matches. The second code returns 0 results?!

Am I being stupid and missing something obvious?

share|improve this question
1  
Parameters are not suitable for choosing tables or columns. Take a look into ef or another ORM to bypass that. –  Grumbler85 Jun 9 '13 at 10:45
9  
You cannot use parameters for the Field and Table names. –  Henk Holterman Jun 9 '13 at 10:45
    
Really you shouldn't be embedding SQL in your executable anyway. Try using stored procedures that can execute sections of script on a selective basis. –  Westie Jun 9 '13 at 10:53
    
Thanks to all of you. I'm just experimenting with all of this at the moment. It's working now, but in the future I will look into stored procedures :) –  Thomas Wormald Jun 9 '13 at 10:58
2  
@Westie: Could you perhaps elaborate on that? A simple "don't do this or that" doesn't really help with understanding why this should or shouldn't be done. –  Caramiriel Jun 9 '13 at 10:58

1 Answer 1

Your parameters are:

@dataToFind
@name
@fieldToCheck
@fieldToReturn

Your Query's CommandText has:

@fieldToReturn
@tableName
@fieldToCheck
@dataToReturn

These do not match. They must match in order to be properly applied.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.