SqlParameter ???

D

Daniel Groh

What is the best way to access a procedure ?

MyCommand.Parameters.Add("@param1",typeof(string));

or

SqlParameter myParam = new SqlParameter();
myParam.Add("@param1",typeof(string));

What do you think ?

--

Atenciosamente,

Daniel Groh
CTF Technologies do Brasil Ltda.
Analista Programador
Fone: 11 3837-4203
E-mail: (e-mail address removed)
 
M

Michael C#

Neither one. typeof(string) should be a SqlDbType (Char, VarChar, etc.)

This method works just fine if you want to use the defaults for your
parameter:

MyCommand.Parameters.Add("@param1", SqlDbType.VarChar, 255).Value = "Hello";

For cases where I don't want defaults used (i.e., .Direction, etc.), I
prefer this method since it's a little cleaner:

SqlParameter myParam = new SqlParameter();
myParam.Size = 255;
myParam.Value = "Hello";
myParam.Direction = ParameterDirection.Output;
MyCommand.Parameters.Add(myParam);
 
D

Daniel Groh

yes thanks, i know about the sqldbtype...thank u so much, but my main
question was about using or not SqlParameter class if u can access via
SqlCommand too...you think is SqlParameter as i saw, not ?

thnaks in advance

--

Atenciosamente,

Daniel Groh
CTF Technologies do Brasil Ltda.
Analista Programador
Fone: 11 3837-4203
E-mail: (e-mail address removed)
 
B

Bob Powell [MVP]

Or even MyCommand.Parameters.Add("param", typeof(string)).Value="Hello
world";

Your two examples are functionally equivalent. Go with what suits your
typing style best.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

BW

Either one will work. However that should be something like:
MyCommand.Parameters.Add("@param1", SqlDbType.VarChar);

Not typeof(...); there is a difference

Anyway, your second example is handy if you want to pass an array of
SqlParameter.

HTH
B
 
M

Michael C#

As I said, splitting it up makes it more readable if you want to set all the
properties of the SqlParameter individually. The other method is more
compact if you're not worried about setting all the properties.
 

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