By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

One Sql Command, Two Result Sets

P: n/a
Hi all,

I'm sending a command via SqlClient, and it returns two result sets. I can
successfully read the first result set, but how can I access the second
result set? Here's an example of my SqlCommand CommandText:

DECLARE @result AS INT\nEXEC @result = master..xp_cmdshell 'dir "c:\*.*" /b
/a-d'\nSELECT @result

When run on SQL Server, as

DECLARE @result as int
EXEC @result = master..xp_cmdshell 'dir "c:\klh\*.*" /b /a-d'
SELECT @result

It returns 2 separate result sets, i.e.:

Result set 1
-------------------
AUTOEXEC.BAT
boot.ini
CONFIG.SYS
IO.SYS
MSDOS.SYS
NTDETECT.COM
ntldr
pagefile.sys
Thumbs.db

Result set 2
---------------------
0

In this instance, a return code of 0 indicates success, 1 indicates failure
of the xp_cmdshell command. How can I access the return code? Maybe I'm
going about it wrong. Thanks.
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
P.S. - Another example is running the sp_helpdb 'databasename' command.
This returns two result sets also. If anyone knows the best way to access
the second result set as well as the first, it would be much appreciated.

Thanks

"Michael C#" <xy*@abcdef.com> wrote in message
news:8s***************@fe10.lga...
Hi all,

I'm sending a command via SqlClient, and it returns two result sets. I
can successfully read the first result set, but how can I access the
second result set? Here's an example of my SqlCommand CommandText:

DECLARE @result AS INT\nEXEC @result = master..xp_cmdshell 'dir "c:\*.*"
/b /a-d'\nSELECT @result

When run on SQL Server, as

DECLARE @result as int
EXEC @result = master..xp_cmdshell 'dir "c:\klh\*.*" /b /a-d'
SELECT @result

It returns 2 separate result sets, i.e.:

Result set 1
-------------------
AUTOEXEC.BAT
boot.ini
CONFIG.SYS
IO.SYS
MSDOS.SYS
NTDETECT.COM
ntldr
pagefile.sys
Thumbs.db

Result set 2
---------------------
0

In this instance, a return code of 0 indicates success, 1 indicates
failure of the xp_cmdshell command. How can I access the return code?
Maybe I'm going about it wrong. Thanks.

Nov 16 '05 #2

P: n/a
Never mind, got the answer. Thank you.
"Michael C#" <xy*@abcdef.com> wrote in message
news:8s***************@fe10.lga...
Hi all,

I'm sending a command via SqlClient, and it returns two result sets. I
can successfully read the first result set, but how can I access the
second result set? Here's an example of my SqlCommand CommandText:

DECLARE @result AS INT\nEXEC @result = master..xp_cmdshell 'dir "c:\*.*"
/b /a-d'\nSELECT @result

When run on SQL Server, as

DECLARE @result as int
EXEC @result = master..xp_cmdshell 'dir "c:\klh\*.*" /b /a-d'
SELECT @result

It returns 2 separate result sets, i.e.:

Result set 1
-------------------
AUTOEXEC.BAT
boot.ini
CONFIG.SYS
IO.SYS
MSDOS.SYS
NTDETECT.COM
ntldr
pagefile.sys
Thumbs.db

Result set 2
---------------------
0

In this instance, a return code of 0 indicates success, 1 indicates
failure of the xp_cmdshell command. How can I access the return code?
Maybe I'm going about it wrong. Thanks.

Nov 16 '05 #3

P: n/a
Use the NextResult() method on the data reader you get back from ExecuteReader. This advances to the next result set.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi all,

I'm sending a command via SqlClient, and it returns two result sets. I can
successfully read the first result set, but how can I access the second
result set? Here's an example of my SqlCommand CommandText:

DECLARE @result AS INT\nEXEC @result = master..xp_cmdshell 'dir "c:\*.*" /b
/a-d'\nSELECT @result

When run on SQL Server, as

DECLARE @result as int
EXEC @result = master..xp_cmdshell 'dir "c:\klh\*.*" /b /a-d'
SELECT @result

It returns 2 separate result sets, i.e.:

Result set 1
-------------------
AUTOEXEC.BAT
boot.ini
CONFIG.SYS
IO.SYS
MSDOS.SYS
NTDETECT.COM
ntldr
pagefile.sys
Thumbs.db

Result set 2
---------------------
0

In this instance, a return code of 0 indicates success, 1 indicates failure
of the xp_cmdshell command. How can I access the return code? Maybe I'm
going about it wrong. Thanks.
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.