[RESOLVED] [2.0] Which Is to use Add or AddWithValue
Hi all :wave:
I just create my first stored procedures
I am using that one in the C# now related to command I have some question that want to use for adding the parameter in the command.
.Add
or
.AddWithValue
C# Code:
private DatabaseHelper dataBaseHelper = new DatabaseHelper("SQLServer");
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public string LastName
{
get
{
return _LastName;
}
set
{
_LastName = value;
}
}
System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand();
sqlCommand = dataBaseHelper.ReturnStoredProcedureCommand();
//Now what TO USE this code
sqlCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
sqlCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = LastName;
//oR this code
sqlCommand.Parameters.AddWithValue("@Name",Name);
sqlCommand.Parameters.AddWithValue("@LastName",LastName);
sqlCommand.ExecuteNonQuery();
Please tell me also that where I Pass the stored procure name??
Thanks
Re: [2.0] Which Is to use Add or AddWithValue
The name of the sproc gets assigned to the CommandText property of the command, where you would otherwise assign the SQL code. You also must set the CommandType to StoredProcedure instead of Text.
You would use AddWithValue in that case. There's no point specifying the parameter type as VarChar when that's what will be interpreted by default from the value itself. You would only use Add if you needed to specify the type, which would be the case if you needed to set the Value multiple times or the type needed to be different from that that would be interpreted from the value specified.
Re: [2.0] Which Is to use Add or AddWithValue
Thanks sir for guiding
I think that AddwithValue is best as compare to the Add
Re: [RESOLVED] [2.0] Which Is to use Add or AddWithValue
AddWithValue is better where it's approrpate, otherwise Add is better.