Solved

ExecuteNonQuery doesn't return Count, what else to use

Posted on 2007-03-26
5
865 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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