Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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?insert into Inventory (Make, PetName, Color) values ('@MakeI', '@PetNameI', '@Color')"
Remove the extra ternary operator, otherwise you will be an Indian!result = command.ExecuteNonQuery() > 0 ? true : false;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question