Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Which Is to use Add or AddWithValue

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [2.0] Which Is to use Add or AddWithValue

    Hi all
    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:
    1. private DatabaseHelper dataBaseHelper = new DatabaseHelper("SQLServer");
    2. public string Name
    3.     {
    4.         get
    5.         {
    6.             return _Name;
    7.         }
    8.         set
    9.         {
    10.             _Name = value;
    11.         }
    12.     }
    13.  
    14.     public string LastName
    15.     {
    16.         get
    17.         {
    18.             return _LastName;
    19.         }
    20.         set
    21.         {
    22.             _LastName = value;
    23.         }
    24.     }
    25.  
    26.  System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand();
    27.  sqlCommand = dataBaseHelper.ReturnStoredProcedureCommand();
    28.  
    29. //Now what TO USE this code
    30.  
    31.  sqlCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
    32.             sqlCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = LastName;
    33.  
    34. //oR this code
    35.  
    36. sqlCommand.Parameters.AddWithValue("@Name",Name);
    37.             sqlCommand.Parameters.AddWithValue("@LastName",LastName);
    38.  
    39.  sqlCommand.ExecuteNonQuery();

    Please tell me also that where I Pass the stored procure name??

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2.0] Which Is to use Add or AddWithValue

    AddWithValue is better where it's approrpate, otherwise Add is better.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width