Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are some explanations:</p> <p><a href="http://forums.asp.net/t/1370191.aspx" rel="nofollow">difference between command Add and AddWithValue</a> </p> <pre><code>Dim cmd as new SqlCommand("SELECT * FROM MyTable WHERE MyDate&gt;@TheDate",conn) cmd.Parameters.Add("@TheDate",SqlDbType.DateTime).Value="2/1/2007" </code></pre> <p>vs</p> <pre><code>cmd.Parameters.AddWithValue("@TheDate","2/1/2007") </code></pre> <p>"Add forces the conversion from string to date as it goes into the parameter. AddWithValue would have simply passed the string on to the SQL Server.</p> <blockquote> <p>When using <code>Parameters.Add</code> - the S qlDbTypeis known at compile time </p> <p>When using <code>Parameters.AddWithValue</code> the method has to box and unbox the value to find out it's type.</p> <p>Additional benefits of the former is that <code>Add</code> is a bit more code safe and will assist against sql injection attacks , code safe in terms that if you try to pass a value that doesn't match the sqldb type defined - the error will be caught in .Net code and you will not have to wait for the round trip back.</p> </blockquote> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx</a></li> </ul> <p><strong>Edit</strong>: </p> <p>example to get an Output-Parameter:</p> <p>C#</p> <pre><code>cmd.Parameters.Add(new SqlParameter("@TheNewId", SqlDbType.Int, int.MaxValue)); cmd.Parameters("@TheNewId").Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); int theNewID = (int)cmd.Parameters("@TheNewId").Value; </code></pre> <p>VB.Net</p> <pre><code>cmd.Parameters.Add(New SqlParameter("@TheNewId", SqlDbType.Int, Int32.MaxValue)) cmd.Parameters("@TheNewId").Direction = ParameterDirection.Output cmd.ExecuteNonQuery() Dim theNewID As Int32 = DirectCast(cmd.Parameters("@TheNewId").Value, Int32) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload