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
Custom Model Binder for a Property
How to Add a Reference to an Unmanaged C++ Project Called by a C# Project
How to Check That a Uri String Is Valid
SQL Injections with Replace Single-Quotation and Validate Integers
Is the Destructor Called If the Constructor Throws an Exception
How to Allow an Anonymous User Access to Some Given Page in MVC
Are Get and Set Functions Popular with C++ Programmers
System.Data.Sqlclient.Sqlexception: Invalid Column Name 'Phone_Types_Phone_Type_Id'
How to Pass Current User Information to All Layers in Ddd
Should I Use Return/Continue Statement Instead of If-Else
Linq: How to Exclude Condition If Parameter Is Null
Why Does My C# Array Lose Type Sign Information When Cast to Object
Error While Using Executenonquery C#
ASP.NET Core MVC - Client-Side Validation for Custom Attribute