Editing
Warning: This response may leave your code vulnerable to
SQL Injection, use this other answer .
Well, let's break it down ...
The question is:
What kind of variable is int
?
varchar
or en-US
?
string
In this case you will need to do a REPLACE in your query replacing the comma with a dot, and after that you'll also need to do a CONVERT or CAST so that nota.nota
is magically transformed into a string
value. This is required because it is not possible to insert a float
into a field that was created to receive string
.
The query would look like this:
SqlCommand cmd = new SqlCommand("UPDATE Notas SET nota = CONVERT(FLOAT, REPLACE('" + nota.nota + "', ',', '.')) WHERE idMateria = " + nota.idMateria + " and idAluno = " + nota.idAluno + ";", con);
Or you could still do float
via C # . before creating the query:
nota.nota = nota.nota.Replace(",", ".");
And thus generate the query without the replace:
SqlCommand cmd = new SqlCommand("UPDATE Notas SET nota = CONVERT(FLOAT, '" + nota.nota + "') WHERE idMateria = " + nota.idMateria + " and idAluno = " + nota.idAluno + ";", con);
Would these be the best options? In my opinion, it does not have a very good smell ...
In my humble opinion, if string
was of type float
would look a lot nicer.
float
As the value is coming from replace
?% < nota.nota
or float
?
If it's arriving as a string, convert to View
on string
, but I'd rather prefer convert to float
on float
already sending object ready.
So your query would look like this:
SqlCommand cmd = new SqlCommand("UPDATE Notas SET nota = " + nota.nota + " WHERE idMateria = " + nota.idMateria + " and idAluno = " + nota.idAluno + ";", con);