Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ExecuteNonQuery doesn't return Count, what else to use

Posted on 2007-03-26
5
Medium Priority
?
872 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:K V
ID: 18797805
it Executes a Transact-SQL statement against the connection and returns the "number of rows affected"
0
 
LVL 21

Expert Comment

by:K V
ID: 18797807
0
 
LVL 21

Expert Comment

by:K V
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:
K V earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

730 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