SQL parameters in ASP.NET, best practise?

Posted on 2007-10-18
Last Modified: 2008-01-09
It is different ways of adding SQL parameters in ASP.NET. I need some info about what I shoul use when adding these parameters. Under is 2 examples that both works fine. The first examples is the fastes to code, but is it as good as the second example? I have also heard aboute using "Parameters.AddWithValue", when is this ment to be used?

Example 1

Cmd.Parameters.Add("@Email", SqlDbType.NVarChar,255).Value = email;

Example 2

SqlParameter Email = new SqlParameter("@Email", SqlDbType.NVarChar,255);
Email.Value = email;
Question by:webressurs
    LVL 26

    Accepted Solution

    there is a post on microsoft forums for Parameters.AddWithValue
    I hope that will help you

    Regarding EG 1 and EG 2 both are the same and i think that EG 1 should be slightly more efficient
    LVL 53

    Assisted Solution

    I don't think there will be any difference in the two ways, except that the code for the second example will be longer than the first one.
    It will both call a implemented constructor (perhaps the same)
    LVL 1

    Author Comment

    Thanks for good answers! I lillte this.. did not really get the different between "Cmd.Parameters.Add" and "Cmd.Parameters.AddWithValue"? In this example it seems like it does the same thing:

    LVL 20

    Assisted Solution

    You can define your parameters more by actual defining SqlParameters i.e.

    SqlParameter s1 = new SqlParameter("@Name","Value");

    this is useful for instance say if you want to specify the direction of the sqlparameter e.g. Input/Output

    LVL 26

    Assisted Solution

    by:Anurag Thakur
    some additions from the MSDN

    "The value can be a string, an XML value, a string, an XmlReader-derived type instance or a SqlXml object."


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now