Error passing parameter to Sqlcommand by parameterizing table

Asked

Viewed 184 times

0

I’m trying to pass parameter in mine select and this bursting the exception:

It is necessary to declare the table variable "@p_tabela".

using (SqlConnection conn = new SqlConnection(connectionString)
{
    try
    {
        conn.Open();
        var sql = "SELECT Id FROM @p_tabela";
        SqlCommand command = new SqlCommand(sql, conn);

        command.Parameters.Add(new SqlParameter("@p_tabela", tabela));

        using (SqlDataReader reader = command.ExecuteReader()) // Estoura erro aqui
        {
            if (reader.Read())
            {
                return true;
            }
        }

        return false;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        conn.Close();
    }
}

I’ve tried this too:

  • command.Parameters.AddWithValue("@p_tabela", tabela);

  • SqlCommand command = new SqlCommand(command, conn); command.Parameters.Add("@p_tabela", SqlDbType.VarChar); command.Parameters["@p_tabela"].Value = tabela;

Second try use from here: https://docs.microsoft.com/pt-br/dotnet/api/system.data.sqlclient.sqlcommand.parameters?view=netframework-4.8 Error image:

Descrição da exceção

  • We need to know more about the error. Anyway I’ve seen in your codes that you use exception the wrong way. Delete this try-catch because he’s of no use. Read more on https://answall.com/search?tab=votes&q=user%3a101%20exce%C3%A7%C3%a3o Should also use more using and not finally.

  • Good morning Leo.. All right.. Dude .. for PARAMETERS of the WHERE argument I use exactly with you demonstrated in the second mode..: command.Parameters.Addwithvalue("@p_table", table);. How little bag of C# my question is.. FROM @p_table WOULD ACCEPT the table name as PARAMETRO?? You know what I mean? To solve this QUICKLY in Usitan mode I would put it like this..: var sql = "SELECT Id FROM " + table; Since you have it as a variable I think it would do no harm.. But as I said a way to resolve NOW :) I don’t know if it would meet your need. Anything just talk :)

  • @Souza, I miss the delay, I did just this to test, I was having the same doubt.... And it really was that... I put Where to confirm and it worked...

1 answer

1

The interpolation system SqlCommand only allows using variables in certain locations, the table name is not an allowed location. So the way would be to do with normal interpolation. But remember this is dangerous in many cases, if the data comes from outside make sure you are very good at it, otherwise you will do wrong things and have safety holes. It would be something like that:

$"SELECT Id FROM {tabela}"

I put in the Github for future reference.

Do not forget the comment above, there is a lot wrong with this code (even because you can see that you do not understand why you are doing certain things, this is dangerous), even if it works.

  • good morning Maniero.. man.. I thought you said :)

  • @Maniero, thank you for the clarification... You can tell me a safe way to do this?

  • Look in my answers, I’ve talked about it a lot. But it’s something that depends on study, there’s no recipe for cake. One of the mistakes I’ve realized you make is copying example.

  • Oops, I’m super happy that Voce answered me exactly how I wanted my question... I will stop doing this, I know it is bad for me and for my learning, but next time, Oce could be a more specific poquinho and give me a reference, a north, so that I study correctly and accurately on the subject, okay?! It’s nothing productive I look for in more than 5,000 responses... Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.