Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SqlCommand

SqlCommand

Scheduled Pinned Locked Moved C#
performancequestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moein serpico
    wrote on last edited by
    #1

    hi where i should use SqlCommand.Parameters.Add()method and where SqlCommand.Parameter.AddWithValue()? is there performance diffrence?

    M P 2 Replies Last reply
    0
    • M moein serpico

      hi where i should use SqlCommand.Parameters.Add()method and where SqlCommand.Parameter.AddWithValue()? is there performance diffrence?

      M Offline
      M Offline
      mersad00
      wrote on last edited by
      #2

      Hi, Specially parameters used to pass data to ur query , as inset update delete even select

      select *
      from t1
      where id = 12

      this query always returns a fixed result, now going to make it gloabal, i mean change the 12 value to needed value, so u use parameters as:

      select *
      from t1
      where id=@id

      now in command line, u must set the parameter, look at this method as a sample:

          private DataTable SelectParameter(int idvalue, bool useWithvalue)
          {
              string constr = "";
              SqlConnection con = new SqlConnection(constr);
              SqlCommand cmd = new SqlCommand("select \* from t1 where id=@id", con);
      
              if (useWithvalue)
              {
                  cmd.Parameters.AddWithValue("@id", idvalue);
                 
              }
              else
              {
                 
                  SqlParameter p = new SqlParameter("@id", SqlDbType.Int);
                  p.Value = idvalue;
                  cmd.Parameters.Add(p);
              }
      
              con.Open();
              DataTable dt = new DataTable();
              dt.Load(cmd.ExecuteReader());
              return dt;
      
          }
      

      in cases that u are going to set the value type of parameter u should use add else u could use both of them.

      1 Reply Last reply
      0
      • M moein serpico

        hi where i should use SqlCommand.Parameters.Add()method and where SqlCommand.Parameter.AddWithValue()? is there performance diffrence?

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        I hear that AddWithValue is kinda safer, but I rarely use it because of the way I do things. It's not a performance issue.

        S 1 Reply Last reply
        0
        • P PIEBALDconsult

          I hear that AddWithValue is kinda safer, but I rarely use it because of the way I do things. It's not a performance issue.

          S Offline
          S Offline
          Searril
          wrote on last edited by
          #4

          I like .AddWithValue just to save keystrokes, but never noticed any performance difference either way.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups