How to write SQL query with parameters?

All kind time of day.

When you run a query to insert a new row into the table, getting themselves mercery parameters and not their values.
void Insert()
{
//the data

//Color
ParamColor SqlParameter = new SqlParameter();
paramColor.DbType = DbType.String;
paramColor.Direction = ParameterDirection.Input;
paramColor.ParameterName = "@Color";
paramColor.Value = item.Color;

command.Parameters.Add(paramColor);

command.CommandText = "insert into Inventory (Make, PetName, Color) values ('@MakeI', '@PetNameI', '@Color')";

result = command.ExecuteNonQuery() > 0 ? true : false;

}


In the code described by only one parameter, the others are described similarly.

In General, in the database I do not fly the values of the param*******.Value and token names, i.e. @MakeI, @PetNameI, @ Color

Sql Server 2012 supports parameters in queries.
September 18th 19 at 23:51
2 answers
September 18th 19 at 23:53
Solution
insert into Inventory (Make, PetName, Color) values ('@MakeI', '@PetNameI', '@Color')"
Well, of course, the base will fly string constants - you do them and send. That's when you C# write the names of string variables in quotes did not take - why are you doing this in SQL language?

PS what is that?!
result = command.ExecuteNonQuery() > 0 ? true : false;
Remove superfluous ternary operator, and you will be a Hindu!
Well, the original I have a function return a bool. And quotes thanks. - jovanny.Cro commented on September 18th 19 at 23:56
Or is there another reason to yell at me for ternary? - jovanny.Cro commented on September 18th 19 at 23:59
: So the fact of the matter! Design (b ? true : false) is absolutely meaningless - instead, one can write b.

That's right: result = command.ExecuteNonQuery() > 0 - Emiliano.Cremin commented on September 19th 19 at 00:02
: Oh, you mean the return of themselves true or false does not make sense? I did not know these subtleties. Thank you! - jovanny.Cro commented on September 19th 19 at 00:05
September 18th 19 at 23:55
Let's say that I was there. And this option does not suit me. This is the first and the second there are more appropriate link https://msdn.microsoft.com/ru-ru/library/c6tdby40(v=vs.110).aspx - jovanny.Cro commented on September 18th 19 at 23:58
In Your example the parameter value is not assigned, you can set its properties. - jovanny.Cro commented on September 19th 19 at 00:01
: paramColor.Value = item.Color; - Emiliano.Cremin commented on September 19th 19 at 00:04
meant Your link to the example - jovanny.Cro commented on September 19th 19 at 00:07
As for Your example, the wrong line
command.CommandText = "insert into Inventory (Make, PetName, Color) values ('@MakeI', '@PetNameI', ")"; - jovanny.Cro commented on September 19th 19 at 00:10

Find more questions by tags SQL Server.NETC#