SqlCommand

B

Brian Toothill

I'm defining an SqlCommand (in C#), with the following members: -

..Connection = myConnection;
..CommandText = "INSERT INTO myTable (columnOne) VALUES (@col1)";
..Parameters.Add("@col1", SqlDbType.Char, 10, "columnOne");

In the Parameters.Add(), is it necessary to specify all that data? Can't I
just specify "@col1"? (I can't at the moment, I get an exception when I
call SqlDataAdapter::Update()).

Why do I need to specify the column data type, size, and name, to avoid an
exception?
 
M

Miha Markic

Hi Brian,

I normally specify all parameters.
The way to find out how the paramteres should be specified I suggest you to
create the adapter at design time (using wizards - in separate project
perhaps) and browse though generated code..
 
W

William \(Bill\) Vaughn

What exception are you getting? In any case, you should define the datatype
and size of VarChar, NVarchar, Char, and NChar as well as VarBinary columns.
This helps the IL move data from point to point. While VB is a bit more
forgiving in this respect, I suspect C# is being anal (it excels at that).
And another thing, why are you using a Char(10)? Is the string ALWAYS 10
bytes long? If not, you should use a VarChar or NVarChar.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
M

Miha Markic

Hi Bill,

William (Bill) Vaughn said:
What exception are you getting? In any case, you should define the datatype
and size of VarChar, NVarchar, Char, and NChar as well as VarBinary columns.
This helps the IL move data from point to point. While VB is a bit more
forgiving in this respect, I suspect C# is being anal (it excels at that).

What would be the difference between VB.NET and C#???
 
W

William \(Bill\) Vaughn

IIRC VB adds more "type morphing" code behind the scenes than C# so you can
move between types more easily--but I could be wrong.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
K

Kevin Yu [MSFT]

Thanks for Bill and Miha's response.

Hi Brian,

When we specify the data type, we should also specify the size of it. The
last one is to specify the source column of the parameter when the data
source is a dataset or datatable. If you need to assign value to the
parameter separately, you can use parameter.Value = value;

For more information about Add() method, please refer to the following link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemDataSqlClientSqlParameterCollectionClassAddTopic.asp

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
P

Pablo Castro [MS]

If you only indicate the parameter name, we'll *try* to infer the data type
based on the value that you provide. In general, if you know the type, it's
better if you indicate it. For input parameters, in most cases, the size is
not needed. Finally, the last argument where you're passing "columnOne" is
only used by the data adapter, so if you're not setting this command object
in one of the DataAdapter command properties then no need to specify it here
(in your case you need it because you're calling adapter.update()
afterwards).

Let me know if you need further details.

--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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