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

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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. :)
 
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.
 
Back
Top