Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

777 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