C# SQLparameters Short Hand

C# SqlParameters Short Hand

You have a bigger constructor:

 command.Parameters.Add(
"@CategoryName", SqlDbType.VarChar, 80).Value = "toasters";

SQLParameter incorrect syntax

your LIKE statements must be inside single quotes

SELECT * FROM Customers WHERE City LIKE '%s%';

What's the best method to pass parameters to SQLCommand?

You can also use AddWithValue(), but be aware of the possibility of the wrong implicit type conversion.

cmd.Parameters.AddWithValue("@Name", "Bob");

C# SQLCommand - Parametrized query is cut short

I don't think it cuts the query, because it mentions the full parameter name @PurseID later in the exception message.

Check if _purse == null by any chance. If so, you must use DBNull.Value instead.

SQL output parameters in C#

@ is missing, update like the following

logID = (int)command.Parameters["@logID"].Value;

SqlParameters to DBContext executesqlcommand

Apart from your syntax, which will throw an error for you, that's pretty much the only way to do it. I have corrected the syntax below:

SqlParameter output = new SqlParameter("editMode", SqlDbType.Bit);
output.Direction = ParameterDirection.Output;

SqlParameter parameter = new SqlParameter("spvId", SqlDbType.Int);
parameter.Value = spvId;

ExecuteProcedure("exec [dbo].[prc_SitePartVrsn_CanLock] @spvId, @editMode OUTPUT", parameter, output);

bool retVal = (bool)output.Value; // same result as using Convert.ToBoolean

edit Unless you want to use something like

var p = new SqlParameter {
ParameterName = "paramName",
DbType = DbType.Bit,
Direction = ParameterDirection.Output
};


Related Topics



Leave a reply



Submit