Solved

VB.net Counting Rows in query doesn't work with SQL Client but does with Oledb

Posted on 2013-01-19
3
459 Views
Last Modified: 2013-01-24
Hi

I am trying to count the number of records in any query using the following code.
I get the error "Syntax error near '('"
The second example where I use Oledb works

(1)
                sSQL = "SELECT Count(*) As MyCount FROM (SELECT [Accounting].[Account Type], [Accounting].[Co_ID] FROM [Accounting])"
     
                Dim cn As New SqlConnection(oConnectionString)
                Dim cmd As New SqlCommand(sSQL, cn)
                cn.Open()

(2)
      sSQL = "SELECT Count(*) As MyCount FROM (SELECT [Accounting].[Account Type], [Accounting].[Co_ID] FROM [Accounting])"

                Dim cn As New OleDbConnection(oConnectionString)
                Dim cmd As New OleDbCommand(sSQL, cn)
                cn.Open()

                oCount_Rows_in_Query = cmd.ExecuteScalar.ToString
0
Comment
Question by:murbro
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 38796850
Try with the following:

"SELECT Count(*) FROM [Accounting]"
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 38796918
you may need to add an alias to the subquery:
  sSQL = "SELECT Count(*) As MyCount FROM (SELECT [Accounting].[Account Type], [Accounting].[Co_ID] FROM [Accounting]) sub_query " 

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 38815846
Thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now