How to correctly send the value of a null variable in an INSERT?

Asked

Viewed 587 times

0

Assuming I have the following code:

public async void InsertBanco(string parametro1, string parametro2)
{
    var b = new BancoN();
    SetBanco(b);
    await b.EnviaMySQLInsert($"INSERT INTO tabela(parametro1, parametro2) " +
    $"VALUES('{parametro1}','{parametro2}') ;");
 }

In some situations the parametro2 can be null, how do I send NULL in the insert?

If I call InsertBanco("Teste", null), for example, sql would look like this:

INSERT INTO tabela(parametro1, parametro2) VALUES('Teste','');

What if I send the string with the value "NULL" (InsertBanco("Teste", "NULL")), this string will be inserted into the database, instead of setting the value to null.

But the right thing would be:

INSERT INTO tabela(parametro1, parametro2) VALUES('Teste', NULL);

How can I fix this problem?


EDIT

Clarifying the problem: in some cases the parametro2 will be null and in some cases will be a string("Text", for example).

When is a string, the code I have works normally. But when I need to set null in the bank, the way it was written the code will never work...

How to write the method InsertBanco so that it can send the correct sql when the parametro2 is null and void and when string normal??

  • Please avoid long discussions in the comments; your talk was moved to the chat

4 answers

5


You can use the query:

INSERT INTO tabela(parametro1, parametro2) VALUES('parametro1', NULLIF ('parametro2', ''));
  • 1

    +1 e + a thank you very much. I did not know of the existence of this NULLIF

  • 1

    I didn’t know him either NULLIF, I think this is the way that best solves my problem.

  • 2

    Put in the table configuration the field with Default/Expression as NULL, then every time the field is empty, it leaves as NULL.

  • In case it would be without the quotes, right? NULLIF (parametro2, '')

  • It depends on what will be used in the query, whether it will be string or not, if it is necessary to quote.

4

Test if parametro2 is null and construct the proper insert. Anything like that:

public async void InsertBanco(string parametro1, string parametro2)
{
    var b = new BancoN();
    SetBanco(b);
    if(parametro2 == null)
    {
        await b.EnviaMySQLInsert($"INSERT INTO tabela(parametro1) " +
                                 $"VALUES('{parametro1}') ;");
        return;
    }
    await b.EnviaMySQLInsert($"INSERT INTO tabela(parametro1, parametro2) " +
                             $"VALUES('{parametro1}','{parametro2}') ;");
 }
  • It works in this case, but if I had more "parametro3" and "parametro4", and these parameters could also receive null, I would have to have an if for each of the possibilities. if(parametro3 == null) ; if(parametro2 == null && parametro3 == null) ; if(parametro2 == null && parametro4 == null) ...

  • 2

    I always answer what is asked :).

  • Is a valid answer :)

  • 1

    See the @Sumback response, it seems to me the most appropriate.

4

Use a conditional expression to check if parametro2 is empty or void

await b.EnviaMySQLInsert( $"INSERT INTO tabela(parametro1, parametro2) VALUES('{parametro1}', { ((string.IsNullOrEmpty(parametro2)) ? "NULL" : ("'" + parametro2 + "'") )}); ");

2

Check whether the parametro2 is empty if you have a pass NULL, otherwise pass the value of parametro2.

await b.EnviaMySQLInsert($"INSERT INTO tabela(parametro1, parametro2) " + 
                         $"VALUES('{parametro1}'," + parametro2 == "" ? "NULL);" : $"'{parametro2}');");

Browser other questions tagged

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