Home All Groups Group Topic Archive Search About

parameters.Add("@f"...) vs parameters.Add(New SqlParameter("@f"

Author
7 Feb 2007 5:02 PM
Rich
da.InsertCommand.Parameters.Add(New SqlParameter("@f", SqlDBtype.Varchar, 10,
"fld1"))
da.Fill(ds, "tbl1")

The statements below will insert a row into "tbl1"  just as well as the
statement above

da.InsertCommand.Parameters.Add("@f", SqlDBtype.Varchar, 10, "fld1")
da.Fill(ds, "tbl1")

Is there any difference if I use ..Add(New SqlParameter(...)  or omit --New
SqlParameter--?    I want to follow the proper convention.  Any suggestions
appreciated.

Thanks,
Rich

Author
8 Feb 2007 12:56 PM
Patrick Steele
In article <2695FF6B-0CEC-4966-8D6F-65F0B4B37***@microsoft.com>,
R***@discussions.microsoft.com says...
> da.InsertCommand.Parameters.Add(New SqlParameter("@f", SqlDBtype.Varchar, 10,
> "fld1"))
> da.Fill(ds, "tbl1")
>
> The statements below will insert a row into "tbl1"  just as well as the
> statement above
>
> da.InsertCommand.Parameters.Add("@f", SqlDBtype.Varchar, 10, "fld1")
> da.Fill(ds, "tbl1")
>
> Is there any difference if I use ..Add(New SqlParameter(...)  or omit --New
> SqlParameter--?    I want to follow the proper convention.  Any suggestions
> appreciated.

They do the exact same thing.  In fact, if you look inside the framework
for the SqlParameterCollection.Add method using Reflector, you'll see
that the second method you listed actually runs the same code as the
first method listed.  :)

Author
8 Feb 2007 4:29 PM
Rich
Very interesting.  So it looks like I could reduce my code by eliminating

New SqlCommand

and the 2 extra parens.    But if I do this instead

Dim prm1, prm2, prm3, prm4() as sqlparameter
....
prm4 = New Sqlparameter(){prm1, prm2, prm3}
for each prm As SqlParameter) in prm4
   da.InsertCommand.Parameters.Add(prm)
Next

Hmmm,  I think I could still get away without using New SqlParameter with
prm1, prm2, prm3.   Interesting.

Show quote
"Patrick Steele" wrote:

> In article <2695FF6B-0CEC-4966-8D6F-65F0B4B37***@microsoft.com>,
> R***@discussions.microsoft.com says...
> > da.InsertCommand.Parameters.Add(New SqlParameter("@f", SqlDBtype.Varchar, 10,
> > "fld1"))
> > da.Fill(ds, "tbl1")
> >
> > The statements below will insert a row into "tbl1"  just as well as the
> > statement above
> >
> > da.InsertCommand.Parameters.Add("@f", SqlDBtype.Varchar, 10, "fld1")
> > da.Fill(ds, "tbl1")
> >
> > Is there any difference if I use ..Add(New SqlParameter(...)  or omit --New
> > SqlParameter--?    I want to follow the proper convention.  Any suggestions
> > appreciated.
>
> They do the exact same thing.  In fact, if you look inside the framework
> for the SqlParameterCollection.Add method using Reflector, you'll see
> that the second method you listed actually runs the same code as the
> first method listed.  :)
>
> --
> Patrick Steele
> http://weblogs.asp.net/psteele
>

AddThis Social Bookmark Button