D
D
Dmitry Gavrilenko2015-03-03 19:55:39
.NET
Dmitry Gavrilenko, 2015-03-03 19:55:39

How to write SQL query with parameters?

Good day to all.
When executing a query to insert a new row into a table, I get the parameter markers themselves and not their values.

void Insert()
{
//данные

//Color
SqlParameter paramColor = 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;

}

Only one parameter is described in the code, the rest are described in the same way.
In general, not param ******. Value values ​​\u200b\u200bare flying to the database, but marker names, i.e. @MakeI, @PetNameI, @Color
Sql Server 2012 supports parameters in queries.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mayorovp, 2015-03-03
@Maddox

insert into Inventory (Make, PetName, Color) values ('@MakeI', '@PetNameI', '@Color')"
Well, of course, string constants will fly to the database - you send them. When you write in C#, you don't quote the names of string variables - why did you do this in SQL?
PS what is this?!
result = command.ExecuteNonQuery() > 0 ? true : false;
Remove the extra ternary operator, otherwise you will be an Indian!

V
Valery Okhotnikov, 2015-03-03
@vox13

Example
https://msdn.microsoft.com/en-us/library/system.da...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question