Solved

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

Posted on 2013-01-19
3
462 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 143

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

860 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