473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

SqlParameter ???

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: dg***@ctf.com.br
Nov 17 '05 #1
5 4583
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);

"Daniel Groh" <ne*********@gmail.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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: dg***@ctf.com.br

Nov 17 '05 #2
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: dg***@ctf.com.br
"Michael C#" <ho***@boutdat.com> escreveu na mensagem
news:uC**************@TK2MSFTNGP09.phx.gbl...
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);

"Daniel Groh" <ne*********@gmail.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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: dg***@ctf.com.br


Nov 17 '05 #3
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.

"Daniel Groh" <ne*********@gmail.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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: dg***@ctf.com.br

Nov 17 '05 #4
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

"Daniel Groh" <ne*********@gmail.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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: dg***@ctf.com.br

Nov 17 '05 #5
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.

"Daniel Groh" <ne*********@gmail.com> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...
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: dg***@ctf.com.br
"Michael C#" <ho***@boutdat.com> escreveu na mensagem
news:uC**************@TK2MSFTNGP09.phx.gbl...
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);

"Daniel Groh" <ne*********@gmail.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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: dg***@ctf.com.br



Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kenneth | last post by:
Can anyone explain me why it is neccesary to include SqlDbType to the SqlParameter. In every example I see, it is done, but no one explaines why. I have for example a date I want to save into my...
1
by: Marcin | last post by:
hi, I would like to ask how to serialize class with members like SqlParameter. I try to use BinaryFormatter. public class Example_Class { public SqlParameter sqlParam; public...
2
by: confused | last post by:
Hi, I want to assign a default value of DBNull.Value to my stored procedure parameter, but also have the ability to overwrite it, so: SqlParameter param = new SqlParameter...
4
by: Jason Huang | last post by:
Hi, I want to use the Sqlparameter and SqlDataAdapter to update my data, and the data will be updated based on two TextBoxes txtCustName and txtCustAddress. Thanks for help. Jason
5
by: Jason Huang | last post by:
Hi, The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is...
0
by: Elliot M. Rodriguez | last post by:
I implemented a very small, basic data access layer for my web application. It works just fine, except for this one bug. One of my methods returns an abstracted dataset. To accomodate X number of...
3
by: Stacey Levine | last post by:
I have a webservice that has the below procedure. Basically a procedure to called a stored procedure and return the results. When I try to call the webservice from my program I get the error. Both...
6
by: Tim Zych | last post by:
' Declare a new parameter object Dim param() As SqlParameter = New SqlParameter(0) {} ' Set this to null and make it an InputOutput parameter param(0) = New SqlParameter("@Something, DBNull.Value)...
2
by: tshad | last post by:
I am trying to build my own DB Class and am trying to set up my parameter list in an array of SqlParameter objects. In my regular code I would do something like:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.