Solved

ExecuteNonQuery doesn't return Count, what else to use

Posted on 2007-03-26
5
861 Views
Last Modified: 2012-06-22
I thought I'd use this construct

Using wrkConnection As New SqlConnection(ConfigurationManager.ConnectionStrings(1).ToString)
Dim wrkSQLCmd As New SqlCommand(varSQLString, wrkConnection)
wrkSQLCmd.Connection.Open()
varRowCnt = wrkSQLCmd.ExecuteNonQuery()
End Using

to execute a Select Count or (simple Select query)...but I'm confused because MSDN says .ExecuteNonQuery() returns -1 except for Delete, update, insert.

What should I be using instead?  I'd like to be able to pass the query string to this construct in a subroutine and have it return the count in a Byref...but mostly I've got to get something that will execute the string and return the count.

Any help with this would be appreciated.

Thanks!
0
Comment
Question by:codequest
  • 4
5 Comments
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18797805
it Executes a Transact-SQL statement against the connection and returns the "number of rows affected"
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18797807
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18797824
i don't understand what is the prob actually.
if you want the row count then you can do executescaler that will return the first value..
eg.
your query:- select  count(*) from emp
so 1st value:- row-count
ExecuteScalar will return only on value that is first column value of the first row in the executed query.
0
 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 500 total points
ID: 18799491
havent checked programtically, but will work....

Pass query: select  count(*) from Table_Name;
YOUR SUB_ROUTINE
Using wrkConnection As New SqlConnection(ConfigurationManager.ConnectionStrings(1).ToString)
Dim wrkSQLCmd As New SqlCommand(varSQLString, wrkConnection)
wrkSQLCmd.Connection.Open()
varRowCnt = wrkSQLCmd.executescaler()
End Using
RETURN varRowCnt
0
 
LVL 2

Author Comment

by:codequest
ID: 18801647
Thanks for inputs.  The "problem" is my acute newbie-itis in this area... :-)
'Preciate the pointers!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding Existing Item to vb.net Project causes Errors 4 32
Store results in vb.net 3 22
C# LINQ ForEach() question 6 28
Calculate number of nights between two dates 5 25
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question