How do I display MSSQL Count?
Code:
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
TomsApplication
Now the count should return 2. How do I retrieve the count from the data reader.
Re: How do I display MSSQL Count?
Something like this.
Code:
While SQLdr.Read
MessageBox.Show(SQLdr[0].ToString()
End While
Re: How do I display MSSQL Count?
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.
Code:
Dim tmpCount As Integer
tmpCount = SQLCmd.ExecuteScalar 'Gets Data
MsgBox(tmpCount)
Did the trick!
Re: How do I display MSSQL Count?
Exactly. ExecuteScalar is the write way of doing it.