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

G

Guest

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
 
P

Patrick Steele

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. :)
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top