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
– Bacco