Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / June 2004

Tip: Looking for answers? Try searching our database.

Capture SQL print output + result set rows?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mario T. Lanza - 24 May 2004 07:09 GMT
Greetings,

I am developing a SQL Server Query Analyzer replacement app to use in
the field to avoid having to purchase licensed copies for a vast
number of machines.

One of the things that Query Analyzer does is capture the output of
PRINT statements in the Messages area.  I have been able to mimic this
by subscribing to the InfoMessages event of the SqlConnection object;
however, this only works when I call the SqlCommand.ExecuteNonQuery
method.

Sometimes the stored procs that I call from within my Query Analyzer
app return rows that I want to capture in a DataGrid.  I was able to
do this using the SqlDataAdapter.Fill method to the table bound to the
grid.

The problem here is merging the two functions.  I want to BOTH capture
result set rows AND the PRINT output text by making only one call to
the stored proc.  I could call the proc twice, once using the
SqlDataAdapter.Fill method and once using the
SqlCommand.ExecuteNonQuery method, but this seems foolish.

1. Any ideas on solving this issue?

2. How about capturing the rows from multiple SELECT statements
executed within a stored proc?

3. Finally, it would be nice to be able to cancel a SqlCommand that is
actively running.  I assume this is done with threads?  Anybody have a
sample snippet of code?

Thanks!
Mario T. Lanza
Clarity Information Architecture, Inc.
William Ryan  eMVP - 24 May 2004 09:09 GMT
Hi Mario:

It's pretty easy, comments inline:

Signature

W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/

> Greetings,
>
[quoted text clipped - 20 lines]
>
> 1. Any ideas on solving this issue?
You can trap the Connection's InfoMessage event.
http://www.knowdotnet.com/articles/connections.html  Even though this is
supposed to trap errors with a severity level < 10 and print messages aren't
errors inthe traditional sense, they'll come back with infomessage.

> 2. How about capturing the rows from multiple SELECT statements
> executed within a stored proc?
If your DB supports batch queries, which it does if you are using SqlServer,
then you the subsequent queries will be in additinoal tables.  If you do a
DataSet.Tables.Count you should see that. (So, if you have a batch query,
just call Adapter.Fill(dataset) and then check your table count.

> 3. Finally, it would be nice to be able to cancel a SqlCommand that is
> actively running.  I assume this is done with threads?  Anybody have a
> sample snippet of code?

Although you may want to use a Thread as well to help make a more responsive
ui (data access apps lend themeselves nicely to multithreading), you can
call SqlCommand's .Cancel method.  You'll note that the documentation says
it 'attempts to cancel a the execution..."
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemdatasqlclientsqlcommandclasstopic.asp

I recommend reading the Remarks section b/c this isn't a guaranteed process
and it often behaves in ways you may not want - but so does cancelling a
command in QA.

If you have any additional questions, please don't hesitate to let me know.
HTH,

Bill

> Thanks!
> Mario T. Lanza
> Clarity Information Architecture, Inc.
Mario T. Lanza - 24 May 2004 21:36 GMT
Bill,

Your suggestions worked fine, thanks.  Now I call all my SQL COMMANDS
using the SqlDataAdapter.Fill(DataSet) method.  Plus I am subscribed
to the SqlConnection.InfoMessage event.  Trouble is I want to capture
the "rows affected" values that usually appear in the SQL Server Query
Analyzer.  I can get this value when I execute my sql statements via a
SqlCommand.ExecuteNonQuery method, but not with the DataAdapter.

Any ideas?
Mario
William Ryan  eMVP - 25 May 2004 03:55 GMT
Fill and Update return integers and will show the records affected although
with fill it may not be what you want b/c it's the nubmer that were changed
if I remember correctly.  After fill, you can interrogate
DataTableName.Rows.Count to get the record count.  Glad the infomessage
worked for you.

You may also want to read my article(s) on datatable .compute which is
another good way to grab aggregate information like count, sum, avg etc,
especially if you need to filter it on anything.
http://www.knowdotnet.com/articles/expressions.html

Signature

W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/

> Bill,
>
[quoted text clipped - 7 lines]
> Any ideas?
> Mario
Mario T. Lanza - 25 May 2004 18:34 GMT
I apologize for keeping this going when I should have gotten it by
now.  Here's the jist the best way I can explain it.

======================================
CREATE PROCEDURE DoTest AS

print 'customers...'
select top 10 * from Customer

print 'customer orders...'
select top 10 * from [CustomerOrder]

print 'updating customer...'
update Customer
set    GivenName = 'John'
where     Surname = 'Smith' and GivenName = 'Jon'
======================================

If I call this procedure in SQL Query Analyzer I get...

======================================
customers...

(10 row(s) affected)

checks...

(10 row(s) affected)

updating customer...

(1 row(s) affected)
======================================
In addition the grids panel populates two grids with 10 rows each.

I want to mimic these messages exactly.  Granted, it's not super
important; my app will work just fine without precise messages;
however, it's more of a thorn in my brain asking, "Why does something
so seemingly easy seem so hard to accomplish?"

Anyway, you've been very helpful.  If there's no easy way, thanks for
your help thus far!
Mario
William Ryan eMVP - 28 May 2004 21:47 GMT
Hi Mario:

I just saw your post. I'm going to have to take another look at it to get it
to output exactly but it shoudl be doable.

hth,

Bill

Signature

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp

> I apologize for keeping this going when I should have gotten it by
> now.  Here's the jist the best way I can explain it.
[quoted text clipped - 39 lines]
> your help thus far!
> Mario
PaulGamblen - 21 Jun 2004 03:02 GMT
Did anybody get an answer or a solution to this problem?
Mary Chipman - 21 Jun 2004 15:23 GMT
What problem?

--Mary

>Did anybody get an answer or a solution to this problem?
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
>----------------------------------------------------------
>    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
>----------------------------------------------------------        

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.