?
Solved

SQL parameters in ASP.NET, best practise?

Posted on 2007-10-18
5
Medium Priority
?
2,121 Views
Last Modified: 2008-01-09
Hello,
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;
Cmd.Parameters.Add(Email);
0
Comment
Question by:webressurs
5 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 1200 total points
ID: 20099883
there is a post on microsoft forums for Parameters.AddWithValue
I hope that will help you
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=68935&SiteID=1

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

Assisted Solution

by:Dhaest
Dhaest earned 400 total points
ID: 20099884
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)
0
 
LVL 1

Author Comment

by:webressurs
ID: 20099963
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:

http://www.aspfree.com/c/a/ASP.NET/Query-Parameters-and-Information-Handling-with-Databases/

0
 
LVL 20

Assisted Solution

by:REA_ANDREW
REA_ANDREW earned 400 total points
ID: 20099976
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

Andrew
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 1200 total points
ID: 20100051
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."

link
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(vs.80).aspx
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

578 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