PC Review


Reply
Thread Tools Rate Thread

confused - exec sp with pure output parameter in ADO.Net

 
 
Robert Wang
Guest
Posts: n/a
 
      19th Jul 2005
I created an sp in northwind in SQL server 2000,

CREATE proc [dbo].[up_getCount]
@Count int output
as
select @Count=count(*)
from northwind.dbo.customers


And ran the following C# code:

using (SqlConnection conn = new
SqlConnection("Server=localhost;Integrated Security=true"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("Northwind.dbo.up_getCount", conn);

cmd.CommandType = CommandType.StoredProcedure;

SqlCommandBuilder.DeriveParameters(cmd);
cmd.ExecuteNonQuery(); // exception got here
}

I got an exception in the line specified. Later I checked out that
cmd.Parameters[1].Direction=ParameterDirection.InputOutput after
SqlCommandBuilder.DeriveParameters was executed. After digging into the
problem, I found several solutions:
1. set cmd.Parameters[1].Direction = ParameterDirection.Output. this
will prevent the value of @Count being checked upon
2. set cmd.Parameters[1].Value = any number or System.DBNull.Value.
3. set the default value for the parameter @Count in the sp, namely,
@Count int = 1 output

these solutions just made me even more confused. I mean in my first
example, who is responsible for detecting the direction and value of the
SqlParameter, the ADO.Net or the database engine? I don't think it is
the ADO.Net because otherwise solution 3 will not work. if it is the db
engine, then what happened when ADO.Net send a System.DBNull to the
dbengine? is is just a "null"?

Can anyone help? Thanks very much.

Robert
MCDBA, MCSD for .Net
 
Reply With Quote
 
 
 
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      19th Jul 2005
Hi Robert,

Why don't you set your parameter in code?
You'll gain speed and more readable code.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

"Robert Wang" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I created an sp in northwind in SQL server 2000,
>
> CREATE proc [dbo].[up_getCount]
> @Count int output
> as
> select @Count=count(*)
> from northwind.dbo.customers
>
>
> And ran the following C# code:
>
> using (SqlConnection conn = new SqlConnection("Server=localhost;Integrated
> Security=true"))
> {
> conn.Open();
> SqlCommand cmd = new SqlCommand("Northwind.dbo.up_getCount", conn);
>
> cmd.CommandType = CommandType.StoredProcedure;
>
> SqlCommandBuilder.DeriveParameters(cmd);
> cmd.ExecuteNonQuery(); // exception got here
> }
>
> I got an exception in the line specified. Later I checked out that
> cmd.Parameters[1].Direction=ParameterDirection.InputOutput after
> SqlCommandBuilder.DeriveParameters was executed. After digging into the
> problem, I found several solutions:
> 1. set cmd.Parameters[1].Direction = ParameterDirection.Output. this will
> prevent the value of @Count being checked upon
> 2. set cmd.Parameters[1].Value = any number or System.DBNull.Value.
> 3. set the default value for the parameter @Count in the sp, namely,
> @Count int = 1 output
>
> these solutions just made me even more confused. I mean in my first
> example, who is responsible for detecting the direction and value of the
> SqlParameter, the ADO.Net or the database engine? I don't think it is the
> ADO.Net because otherwise solution 3 will not work. if it is the db
> engine, then what happened when ADO.Net send a System.DBNull to the
> dbengine? is is just a "null"?
>
> Can anyone help? Thanks very much.
>
> Robert
> MCDBA, MCSD for .Net



 
Reply With Quote
 
Robert Wang
Guest
Posts: n/a
 
      20th Jul 2005
Miha,

Thanks for your suggestion. Anyway, I'm aware of the performance issue
here. I wrote the code just to discover how
SqlCommandBuilder.DeriveParameters works. ^_^

Robert

Miha Markic [MVP C#] wrote:
> Hi Robert,
>
> Why don't you set your parameter in code?
> You'll gain speed and more readable code.
>


-- Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info "Robert
Wang" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>>I created an sp in northwind in SQL server 2000,
>>
>> CREATE proc [dbo].[up_getCount]
>> @Count int output
>> as
>> select @Count=count(*)
>> from northwind.dbo.customers
>>
>>
>> And ran the following C# code:
>>
>> using (SqlConnection conn = new

SqlConnection("Server=localhost;Integrated
>> Security=true"))
>> {
>> conn.Open();
>> SqlCommand cmd = new SqlCommand("Northwind.dbo.up_getCount", conn);
>>
>> cmd.CommandType = CommandType.StoredProcedure;
>>
>> SqlCommandBuilder.DeriveParameters(cmd);
>> cmd.ExecuteNonQuery(); // exception got here
>> }
>>
>> I got an exception in the line specified. Later I checked out that
>> cmd.Parameters[1].Direction=ParameterDirection.InputOutput after
>> SqlCommandBuilder.DeriveParameters was executed. After digging into the
>> problem, I found several solutions:
>> 1. set cmd.Parameters[1].Direction = ParameterDirection.Output. this

will
>> prevent the value of @Count being checked upon
>> 2. set cmd.Parameters[1].Value = any number or System.DBNull.Value.
>> 3. set the default value for the parameter @Count in the sp, namely,
>> @Count int = 1 output
>>
>> these solutions just made me even more confused. I mean in my first
>> example, who is responsible for detecting the direction and value of

the
>> SqlParameter, the ADO.Net or the database engine? I don't think it

is the
>> ADO.Net because otherwise solution 3 will not work. if it is the db
>> engine, then what happened when ADO.Net send a System.DBNull to the
>> dbengine? is is just a "null"?
>>
>> Can anyone help? Thanks very much.
>>
>> Robert
>> MCDBA, MCSD for .Net

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax for exec sp w/ varchar parameter as cbo rowsource? =?Utf-8?B?TktUb3dlcg==?= Microsoft Access ADP SQL Server 7 20th Nov 2007 08:55 PM
Confused with Output cache set programmatically SharpSmith@gmail.com Microsoft ASP .NET 0 10th Nov 2006 05:50 PM
Output report help please - I'm very confused =?Utf-8?B?S29uYUFs?= Microsoft Access VBA Modules 3 2nd Aug 2006 04:58 PM
Difference Between Output Parameter And Return Parameter - SP =?Utf-8?B?U2FuamF5IEFncmF3YWw=?= Microsoft ADO .NET 4 30th Mar 2004 07:59 AM
DataReader with a return parameter and an OUTPUT parameter. Kevin Burton Microsoft ADO .NET 7 28th Nov 2003 06:14 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:10 AM.