Click to See Complete Forum and Search --> : How do I display MSSQL Count?


fallnwrld
March 5th, 2009, 11:02 AM
Imports System.Data.SqlClient

Dim SQLStr, ConnString As String

ConnString = "Data Source=Server;Initial Catalog=CDAD;Integrated Security=True"
Dim SQLConn As New SqlConnection() 'The SQL Connection
Dim SQLCmd As New SqlCommand() 'The SQL Command
Dim SQLdr As SqlDataReader 'The Local Data Store
SQLConn.ConnectionString = ConnString 'Set the Connection String

SQLStr = "SELECT COUNT(*) FROM TABLE WHERE strComputerName = 'TomsCPU' AND strApplication = 'TomsApplication'"
SQLConn.Open() 'Open the connection
SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
SQLCmd.CommandText = SQLStr 'Sets the SQL String
SQLdr = SQLCmd.ExecuteReader 'Gets Data


Table Looks Like
TABLE
strComputerName
TomsCPU
GarysCPU
TomsCPU

strOffice
Florida
Texas
FLorida

strApplication
TomsApplication
ExcelX
TomsApplicationNow the count should return 2. How do I retrieve the count from the data reader.

Shuja Ali
March 5th, 2009, 12:19 PM
Something like this.
While SQLdr.Read
MessageBox.Show(SQLdr[0].ToString()
End While

fallnwrld
March 5th, 2009, 12:22 PM
Thanks for your reply. I was able to figure it out a different way, it looks like the problem was i was using the datareader.


Dim tmpCount As Integer
tmpCount = SQLCmd.ExecuteScalar 'Gets Data
MsgBox(tmpCount)


Did the trick!

Shuja Ali
March 5th, 2009, 12:53 PM
Exactly. ExecuteScalar is the write way of doing it.